各位兄台,不知有没有办法将传入到存储过程中的字符串拆分并付给数组,现在小弟遇到一件事,如下,
string strList = "ID1;ID2;ID3;ID4;……"
我如何能存储过程中处理根据传进来的ID得到一个相应的数据集,(首先要去掉字符ID,得到一个整形数组)
,很急,谢谢大家

解决方案 »

  1.   

    只要能得到如
    select * from 表名 where ID= 数组[0] or ID = 数组[1] or ……
    就行了
      

  2.   

    select * from 表名 where ';ID'+ID+';' like ';ID1;ID2;ID3;ID4;'类似上面的语句可以达到你的目的
      

  3.   

    举个例子
    select * from 
    --(...)a替换成你的表名称
    (
    select '1' as id union  select '2' union select '3'  union select  '4'
    )a
    where charindex(
    ';ID'+id+';' , ';ID1;ID2;ID3;')>0
      

  4.   

    使用表变量实现
    参考一下函数
    @sstr是分割符字符串
    @B是传入字符串例如:字符串为"a;b;c;d"
    调用:select * from fn_SplitToEnum(';','a;b;c;d')
    create function fn_SplitToEnum(@sstr varchar(10),@B text)
    returns @result table(result varchar(255))
    asbegin--declare @sstr varchar(20),@B varchar(255)
    --select @sstr=',', @B=','
    --declare @result table(result varchar(255))
    declare @tmp varchar(255)
    declare @s_point int,
    @t_point int,
    @t int,@tmpsublen int declare @pos int

    --临时变量
    select @s_point=0,@t_point=0,
    @pos=0

    select @t_point=charindex(@sstr,@B,@s_point)
    --print @t_point
    --print @pos
    while (1=1)
    begin

    if @t_point>0
    begin
    set @tmpsublen=@t_point-@s_point
    set @tmp=rtrim(ltrim(substring(@B,@s_point,@tmpsublen)))
    --print '[0]'+str(@s_point)+@tmp
    insert @result values(@tmp)
    end
    else
    begin
    set @tmpsublen=datalength(@B)-@t_point+1
    set @tmp=rtrim(ltrim(substring(@B,@s_point,@tmpsublen)))
    --print '[1]'+@tmp
    insert @result values(@tmp)
    end
    if @t_point<=0 
    goto tend
    select @s_point=@t_point+1
    select @t_point=charindex(@sstr,@B,@s_point)
    end

    tend:
    return
    end
      

  5.   

    http://blog.csdn.net/CNscIp/archive/2004/09/09/99829.aspx