在存储过程中查询某多个ID值时遇到的问题:declare @ids nvarchar(1000) 
set @ids='1,2,3'
select * from table where id in '+@ids+'  //这样执行会遇到转换错误,但如果该条语句赋给一个变量在执行就没问题,我希望能在不将这条语句赋值的前提下执行

解决方案 »

  1.   


    declare @ids nvarchar(1000)
    set @ids='1,2,3'
    select * from table where charindex(','+id+',',','+@ids+',')>0
      

  2.   

    declare @ids nvarchar(1000) 
    declare @s varchar(5000)
    set @ids='1,2,3' 
    set @s='select * from test where id in ('+@ids+')'
    print @s
    exec(@s) 
      

  3.   

    可以参见
    http://topic.csdn.net/u/20080726/23/45568f53-069f-472e-80b0-1361a72109de.html
      

  4.   


    declare @ids nvarchar(1000)
    set @ids='1,2,3'
    select * from table where charindex(','+rtrim(id)+',',','+@ids+',')>0