当前位置: 代码迷 >> Sql Server >> EXEC输出调用外部变量要怎么处理呢
  详细解决方案

EXEC输出调用外部变量要怎么处理呢

热度:40   发布时间:2016-04-27 12:52:25.0
EXEC输出调用外部变量要怎么办呢?
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
  相关解决方案