Output Values

One advantage that the system stored procedure sp_executesql() has over the standard EXECUTE() statement is that it allows a variable declared in the string statement to be returned to the calling routine.

For example,


DECLARE @count as INT DECLARE @SQLx as nvarchar(200)
SET @SQLx =N'SELECT @count = COUNT(*) FROM Variables'
EXEC sp_executesql @SQLx, N'@count INT OUTPUT', @count= @count OUTPUT 
SELECT @count