有没有办法select所有行内容合并为一个字符串?表t
id name
----------------
1 aaa
2 bbb
3 cc
4 d需要的结果:
id name
----------------
1,2,3,4 aaa,bbb,cc,d

解决方案 »

  1.   

    ----------------------------------------------------
    /*如何将一列中所有的值一行显示
    数据源
      a
      b
      c
      d
      e
    结果
    a,b,c,d,e
    */create table tb(col varchar(20))
    insert tb values ('a')
    insert tb values ('b')
    insert tb values ('c')
    insert tb values ('d')
    insert tb values ('e')
    go--方法一
    declare @sql varchar(1000)
    set @sql = ''
    select @sql = @sql + t.col + ',' from (select col from tb) as t
    set @sql='select result = ''' + left(@sql , len(@sql) - 1) + ''''
    exec(@sql)
    /*
    result     
    ---------- 
    a,b,c,d,e,
    */--方法二
    declare @output varchar(8000)
    select @output = coalesce(@output + ',' , '') + col from tb
    print @output
    /*
    a,b,c,d,e
    */drop table tb-------------
      

  2.   

    --> Title  : Generating test data 
    --> Author : wufeng4552
    --> Date   : 2010-02-04 13:57:59
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([id] int,[name] nvarchar(3))
    Insert tb
    select 1,N'aaa' union all
    select 2,N'bbb' union all
    select 3,N'cc' union all
    select 4,N'd'
    Go
    declare @s varchar(1000)
    select @s=isnull(@s+',','')+ltrim(ID)from tb
    select @s=isnull(@s+',','')+ltrim([Name])from tb
    select @s
    /*
    1,2,3,4,aaa,bbb,cc,d(1 個資料列受到影響)
    */
      

  3.   


    declare @s varchar(1000) declare @s1 varchar(1000)
    select @s=isnull(@s+',','')+ltrim(ID)from tb
    select @s1=isnull(@s1+',','')+ltrim([name])from tb
    select @s,@s1id                             name
    ------------------------ ---------------------------
    1,2,3,4,5                      a,b,c,d,e(1 行受影响)
      

  4.   

    不好意思,我不是太了解问题的背景,如果不是非SQL不行的情况下,是否可以读取后,再程序中拼串,这样效率是否更好一些
      

  5.   

    declare @t table(id int,name varchar(3))
    insert @t select 1,'aaa'
    insert @t select 2,'bbb' 
    insert @t select 3,'cc' 
    insert @t select 4,'d' declare @id varchar(20),@name varchar(20)
    select @id='',@name=''select @id=@id+','+cast(id as varchar(10)),@name=@name+','+name
    from @tselect right(@id,len(@id)-1) as id,right(@name,len(@name)-1) as name
    /*
    id                   name
    -------------------- --------------------
    1,2,3,4              aaa,bbb,cc,d
    */
      

  6.   

    在数据量比较少的情况下:
    可以用生成一个字符串的方法为:
    declare @s varchar(1000)
    select @s=isnull(@s+',','')+ltrim(ID)from tb
    select @s=isnull(@s+',','')+ltrim([Name])from tb
    select @s如果字符数据太长,那就不行了.
      

  7.   

    不过要是数据量很大,通过sql还是不太好,个人感觉,关键还是看问题的背景和应用的场景,要是没有效率问题,到没什么问题
      

  8.   


    Create table tb([id] int,[name] nvarchar(3))
    Insert tb
    select 1,N'aaa' union all
    select 2,N'bbb' union all
    select 3,N'cc' union all
    select 4,N'd'
    Goselect top 1id=SUBSTRING((select ','+cast(id as varchar) from tb for xml path('')),2,100000),
    name=SUBSTRING((select ','+name from tb for xml path('')),2,100000)from tb
      

  9.   

    INSERT INTO 新表 idname   select id,name from 旧表