存储过程是:
        Create Proc Test
(IDList nvarchar(30))
 as
   begin
    Select * from TestTable where ID in (@IDList)
   end传递参数 '1','2','3','4'这样传递进去会立即报错,说 参数过多如果要实现这样的功能,存储过程要怎么改?或者说出入的参数如何整整?

解决方案 »

  1.   

    传参数改为 1,2,3,4条件改为 ','+@IDList+',' like '%'+rtrim(ID)+'%'或用charindex/patindex
    也可以用exec('...... where ID in('+@IDList+')')
      

  2.   

    Create   Proc   Test (IDList   nvarchar(30)) 
      as 
          begin 
            execute('Select   *   from   TestTable   where   ID   in (' + @IDList + ')') 
          end
      

  3.   

    Create   Proc   Test 
    (IDList   nvarchar(30)) 
    as 
      begin 
            Select   *   from   TestTable   where charindex(','+ltrim(ID)+',',','+@IDList+',')>0 
      end
      

  4.   

      Create   Proc   Test 
    (IDList   nvarchar(30)) 
      as 
          begin 
            EXEC('Select   *   from   TestTable   where   ID   in   ('+@IDList+')') 
          end或者改用CHARINDEX
    Select   *   from   TestTable   where CHARINDEX(','+LTRIM(ID)+',',','+@IDList+',')>0
      

  5.   

    create proc Test
    (IDlist varchar(30))
    as
    beginExec('select * from Testtb where ID in('+IDlist+')');end
      

  6.   


    那么传入参数 应该如何传入 ? exec Test ''1','2','3''这样嘛?
      

  7.   

    exec('Select   *   from   TestTable   where   ID   in   ('+@IDList+')') 
      

  8.   

    比如传递 一个 'A','B','c'
    不是数字,是字符串性质的,传到里面去用 in 执行查询
      

  9.   

    你参考吧---------------------------------------------
    --> Author : jinjazzli
    --> Target : ---->1000
    --> Date   : 2009-12-08 14:41:40
    --> Version: SQL Server 2005
    ---------------------------------------------
        
    --> 测试数据: @tb
    create table #t(id int,name varchar(2))
    insert into #t
    select 1,'aa' union all
    select 2,'aa' union all
    select 3,'aa' union all
    select 4,'bb' union all
    select 5,'cc' union all
    select 6,'cc' union all
    select 7,'ee' union all
    select 8,'ff' union all
    select 9,'ff'declare @s varchar(10)
    set @s='1,3,4'exec('Select   *   from   #t where  ID in ('+@s+')')id          name
    ----------- ----
    1           aa
    3           aa
    4           bb(3 行受影响)
    drop table #t
      

  10.   

    create proc Test
    (IDlist varchar(30))
    as
    beginExec('select * from Testtb where ID in('+IDlist+')');end
      

  11.   

    呀呀,楼上的我都看了。现在关键是 传递的不是 1,2,3啦,是字符。一般我们用in 查询的时候,如果是数字,是这样 in (1,2,3),啊,那就可以像大家推荐的哪样 写啊。我的不是这样的,传递到in里面的是一个个的字符 ,用 in 是这样查询的 in ('110','112','222').现在我遇到难题是 ,传递参数进来的时候,不能 这样传递 '110','112','222',比如执行 exec Test ''110','112','222'',这样会报错的。 正确的传递参数的方法是什么呢?
      

  12.   

    哈哈,最后把参数换成 A,B,C在转换就搞定啦