1.EXEC使用
EXEC命令两种用种执行存储程另种执行态批处理所讲都第二种用
面先使用EXEC演示例,代码1
DECLARE @TableName VARCHAR(50),@Sql NVARCHAR (MAX),@OrderID INT;
SET @TableName = 'Orders';
SET @OrderID = 10251;
SET @sql =
'SELECT * FROM '+QUOTENAME(@TableName) +'WHERE OrderID = '+
CAST(@OrderID AS VARCHAR(10))+' ORDER BY ORDERID DESC'
EXEC(@sql);
sp_executesql命令SQL Server引入比EXEC命令晚些主要重用执行计划提供更支持
EXEC作鲜明比,我看看用代码1代码EXEC换sp_executesql,看看否我所期望结
DECLARE @TableName VARCHAR(50),@sql NVARCHAR(MAX),@OrderID INT ,@sql2 NVARCHAR(MAX);
SET @TableName = 'Orders ';
SET @OrderID = 10251;
SET @sql = 'SELECT * FROM '+QUOTENAME(@TableName) + ' WHERE OrderID = '+CAST(@OrderID AS VARCHAR(50)) + ' ORDER BY ORDERID DESC'
EXEC sp_executesql @sql
先申明out参数,再执行过程,记得过程中out参数前的冒号。