Dynamic SQL

从Dynamic SQL 读取返回值

DECLARE @query as nvarchar(200), @count int;
SET @query = N\'SELECT @count = COUNT(*)  FROM Orders\';

EXEC sp_executesql @query, 
                   N\'@count int OUTPUT\', 
                   @count OUTPUT

SELECT @count 

向Dynamic SQL传参数

DECLARE @ParmDefinition nvarchar(500)=\'@ID INT\', @SQLCmd nvarchar(max),@ID INT=0

SET @SQLCmd=\'SELECT * FROM Sales.Orders WHERE OrderID=@ID\'

EXEC sp_executesql @SQLCmd,@ParmDefinition,@ID

版权声明:本文为sqlzh原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sqlzh/p/9606282.html