表 tab Aid  name value
1   100   a
2   100   b
3   200   c
4   200   d
5   300   e要求得到结果name valuelist
100    ab
200    bc
300    e  求教,  用存储过程实现最好,  其他的也行.

解决方案 »

  1.   


    select 
    Name,
    valuelist=(select ''+value from A where Name=t.Name for xml path(''))
    from A as t
      

  2.   

    use Tempdb
    go
    --> --> 
     
    if not object_id(N'A') is null
    drop table A
    Go
    Create table A([id] int,[name] int,[value] nvarchar(1))
    Insert A
    select 1,100,N'a' union all
    select 2,100,N'b' union all
    select 3,200,N'c' union all
    select 4,200,N'd' union all
    select 5,300,N'e'
    Go
    select 
    Name,
    valuelist=(select ''+value from A where Name=t.Name for xml path(''))
    from A as t
    GROUP BY Name
    /*
    Name valuelist
    100 ab
    200 cd
    300 e
    */