请教如何将以下的字串资料转成如表a
111,222,333,444,555
表a:
111
222
333
444
555

解决方案 »

  1.   

    create table #t(str varchar(20))
    go
    declare @sql varchar(2000)
    declare @str varchar(200)
    set @str='111,222,333,444,555'
    set @str=replace(@str,',',' union select ')
    set @sql='insert into #t select '+@str
    print @sql
    exec(@sql)
    select * from #t
    drop table #t
    -----------------------
    str
    -----------------------
    111
    222
    333
    ------------------------
    insert into #t select 111 union select 222 union select 333 union select 444 union select 555(5 行受影响)(5 行受影响)
    444
    555
      

  2.   

    if object_id('a') is not null
    drop table a
    create table a(id varchar(50))create proc Pcomma
    @s varchar(50)
    as
    declare @idex int,@s1 varchar(50)
    set @s1=''
    while(charindex(',',@s)!=0)
    begin
    set @idex=charindex(',',@s)
    select @s1=substring(@s,1,@idex-1)
    set @s=substring(@s,@idex+1,len(@s))
    insert into a
    select @s1
    end
    set @s1=@s
    insert into a
    select  @s1exec Pcomma '1,23,56,44,tt'
      

  3.   

    declare @a table(name varchar(80))
    insert @a 
    select '111'
    union all
    select '222'
    union all
    select '333'
    union all
    select '444'declare @name varchar(80)
    set @name=''
    select @name=@name+','+name from @a
    set @name=stuff(@name,1,1,'')
    print @name/*(所影响的行数为 4 行)111,222,333,444
    */
      

  4.   

    请问zjexe(比正牌多两个横):
    若您的方法使用函数中,可行吗?有需要修改吗?谢谢
      

  5.   

    可是我用了之后,出现以不信息..
    在函数中的'EXECUTE STRING' 使用副作用或时间相依运算子无效。
      

  6.   

    declare @str varchar(20)
    set @str='111,222,333,444'declare @n int
    set @n=0
    set @str=@str+','declare @a table(name varchar(80))
    while(@n<len(@str)/4)
    begin
    insert @a 
    select substring(@str,4*@n+1,3)
    set @n=@n+1
    end
    select * from @a/*
    name                                                                             
    -------------------------------------------------------------------------------- 
    111
    222
    333
    444(所影响的行数为 4 行)
    */