Suppose I have a query stored in a variable like this (it\'s actually dynamically populated and more complex, but this is for demonstration purposes):
DECLAR
Try this:
DECLARE @Query NVARCHAR(1000) = 'SELECT @C = COUNT(*) FROM dbo.MyTable' DECLARE @Count AS INT EXEC sp_executesql @Query, N'@C INT OUTPUT', @C=@Count OUTPUT IF (@Count > 0) BEGIN END