Have table TB, as follows: ID value ---------- 1 AA1 BB2 AAA2 BBB2 CCC
Need results: ID value ------ ----------- 1 AA, BB2 AAA, BBB, CCC
That is, group by ID, seeking Value and (string addition)
The general handling method of this problem is to write a polymeric function: create function dbo.f_str (@ID int) returns varchar (8000) asbegin declare @r varchar (8000) set @ r = '' select @ r = @ r ', ' value from tb where id = @ id return stuff (@ r, 1, 1,' ') endGO
- Call function select id, value = dbo.f_str (id) from TB Group By ID