- SQL code
--例子declare @a nvarchar(100),@b int;set @b=20;set @[email protected]';exec(@a)/*这样的输出提示没有申明变量Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@b".[email protected]?*/
------解决方案--------------------
sp_executesql
下面的示例创建并执行一个简单的 SELECT 语句,其中包含名为 @level 的嵌入参数。
execute sp_executesql
N'select * from pubs.dbo.employee where job_lvl = @level',
[email protected] tinyint',
@level = 35
------解决方案--------------------
- SQL code
declare @a int,@b int, @GSResule nvarchare(1000)set @a=3,set @b=4,select @GSResule='select gs from test where gs > @a and gs< @b' exec sp_executesql @GSResule, [email protected] int,@b int',@a,@b