declare @Tmp varchar(110),@id int,@sd int
set @sd=1
    set @Tmp = 'select @id=id from [sound] where id='+ str(@sd)
--select @id=0
exec (@Tmp)
if isnull(@id,0) = 0
select 'a'
else
select 'b'为什么老是提示: 必须声明标量变量 "@id"。

解决方案 »

  1.   

    declare @Tmp nvarchar(110),@id int,@sd int
    set @sd=1
    set @Tmp = 'select @id=id from [sound] where id='+ str(@sd)
    exec sp_executesql @Tmp,N'@id int output', @id output  
    if isnull(@id,0) = 0
    select 'a'
    else
    select 'b' 
      

  2.   

    动态sql中不能使用表(变量),用sp_executesql带输出的参数
      

  3.   

    declare @Tmp nvarchar(110),@id int,@sd int 
    set @sd=1 
        set @Tmp = 'select @id1=id from sound where id='+ cast(@sd as varchar) 
    execute sp_executesql @Tmp,N'@id1 int output',@id1=@id output
    if isnull(@id,0) = 0 
    select 'a' 
    else 
    select 'b' 
      

  4.   

    这是动态sql语句传入参数的问题:use northwind
    declare @Tmp nvarchar(110),@id int,@sd int
    set @sd=3
    set @Tmp = 'select @id2=Employeeid from Employees where Employeeid='+ cast(@sd as varchar)
    print @tmp
    exec sp_executesql @Tmp,N'@id2 int output', @id output
      
    if isnull(@id,0) <> 0
    select @id
    else
    select '没有找到'