有3行数据第一行:a,b,c
第二行:b,c,d,e,f
第三行:sa,4t,x我想合并为一行,合并的结果是:
a,b,c,b,c,d,e,f,sa,4t,x谢谢

解决方案 »

  1.   

    declare @s varchar(8000)
    select @s=isnull(@s+',','')+col from tb
    select @s
      

  2.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (col nvarchar(10))
    insert into [tb]
    select 'a,b,c' union all
    select 'b,c,d,e,f' union all
    select 'sa,4t,x'
    declare @s varchar(8000)
    select @s=isnull(@s+',','')+col from tb
    select @s
    /*
    a,b,c,b,c,d,e,f,sa,4t,x(1 個資料列受到影響)
    */
      

  3.   


    declare @sql varchar(8000)
    select @sql =isnull(@sql +',','')+字段名 from 表名
    select @sql 
      

  4.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([code] varchar(9))
    insert [tb]
    select 'a,b,c' union all
    select 'b,c,d,e,f' union all
    select 'sa,4t,x'declare @sql varchar(8000)
    select @sql = ''
    select @sql = @sql+','+ [code] from [tb]select stuff(@sql,1,1,'')
    ----------------
    a,b,c,b,c,d,e,f,sa,4t,x
      

  5.   

    ---------------------------------------------
    --> Author : js_szy
    --> Target : ---->★★★
    --> Date   : 2009-12-15 16:56:29
    --> Version: SQL Server 2005
    ---------------------------------------------
        
    --> 测试数据: @tb
    declare @tb table (id varchar(22)  )
    insert into @tb
    select 'a,b,c' union all
    select 'b,c,d,e,f' union all
    select 'sa,4t,x' 
     
    declare @s varchar(50)select @s=isnull(@s+',','')+id from @tbselect @s
    --------------------------------------------------
    a,b,c,b,c,d,e,f,sa,4t,x
      

  6.   

    --> 测试数据:@tb
    declare @tb table([name] varchar(9))
    insert @tb
    select 'a,b,c' union all
    select 'b,c,d,e,f' union all
    select 'sa,4t,x'declare @ varchar(8000)
    select @=isnull(@+',','')+[name] from @tb
    select @
    /*
    (3 行受影响)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    a,b,c,b,c,d,e,f,sa,4t,x(1 行受影响)
    */
      

  7.   

    ---测试数据
    if object_id [tb] is not null drop table [tb]
    create table [tb](col varchar(10)
    insert into [tb]
    select 'a,b,c' union all
    select 'b,c,d,e,f' union all
    select 'sa,4t,x'declare @s varchar(8000)
    select @s=isnull(@s+',','')+col from tb
    select @s
    /*
    a,b,c,b,c,d,e,f,sa,4t,x*/
      

  8.   

    ---测试数据 
    if object_id [tb] is not null drop table [tb] 
    SQL codecreate table [tb](col varchar(10)
    insert into [tb]
    select 'a,b,c' union all
    select 'b,c,d,e,f' union all
    select 'sa,4t,x'declare @s varchar(8000)
    select @s=isnull(@s+',','')+col from tb
    select @s
    /*
    a,b,c,b,c,d,e,f,sa,4t,x*/