例如:id   name
001  张三
002  李四
003  老五
.    .
.    .
.    .
我想得到的是name字段所有值,值之间与"、"相连。
得到的结果是:张三、李四、老五。

解决方案 »

  1.   

    create table #t(id int, name varchar(100))insert into #t
    select '001','张三' union all
    select '002','李四' union all
    select '003','老五'declare @s varchar(8000)set @s=''select @s=@s + ',' + name from #tset @s=stuff(@s,1,1,'')select @sdrop table #t--返回:张三,李四,老五
      

  2.   

    declare @tab table(id1 varchar(10),name1 varchar(20))insert @tab values('001','张三')
    insert @tab values('002','李四')
    insert @tab values('003','老五')select * from @tabdeclare @str varchar(5000)
    set @str=''select @str=@str+name1+'、' from @tabselect @str
      

  3.   

    create table #t(id int, name varchar(100))insert into #t
    select '001','张三' union all
    select '002','李四' union all
    select '003','老五'declare @s varchar(8000)set @s=''select @s=@s + '、' + name from #tset @s=stuff(@s,1,1,'')select @sdrop table #t--返回:张三、李四、老五