Dynamic SQL 动态SQL语句的两种用法
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