我有一个表  a
name id
车务  1
机务  2
供电  3
供电  4
车辆  5
车务  6用一条sql语句让显示结果为:
车务,机务,供电,车辆,车务   也就是字符串相加。  显示一条记录

解决方案 »

  1.   

    读出来后遍历相加。用sql不会。等高人
      

  2.   

    declare @str varchar(8000)
    set @str= ''
    select @str=@str+namefrom table 
    print @str
      

  3.   


    加逗号
    declare @str varchar(8000)
    set @str= ''
    select @str=@str+','+colName from tbl_emp
    set @str=stuff(@str,1,1,'')
    print @str
      

  4.   

    declare @tbl table(name nchar(2), id int)
    insert into @tbl
    select '车务',  1 union all
    select '机务',  2 union all 
    select '供电',  3 union all 
    select '供电',  4 union all 
    select '车辆',  5 union all 
    select '车务',  6 declare @ret varchar(8000)
    set @ret = ''
    select @ret = @ret + ',' + name from @tbl 
    set @ret = stuff(@ret,1,1,'')
    select @ret--车务,机务,供电,供电,车辆,车务
      

  5.   

    --> 测试数据: [s]
    if object_id('[s]') is not null drop table [s]
    create table [s] (name varchar(4),id int)
    insert into [s]
    select '车务',1 union all
    select '机务',2 union all
    select '供电',3 union all
    select '供电',4 union all
    select '车辆',5 union all
    select '车务',6declare @sql varchar(1000)
    select @sql=isnull(@sql+',','')+name from [s]
    select @sql
      

  6.   

    简单sql好像难以实现,放程序中的话加变量很容易实现阿。