mssql sql server 变相实现函数 group_concat 的功能


摘要:
下文讲述使用函数 stuff变相实现group_concat函数功能的方法实现,如下所示:
实验环境:sql server 2008 R2


create table A(keyId int,info1 varchar(30))

insert into A values(10,'maomao365.com')
insert into A values(21,'www.maomao365.com')
insert into A values(21,'tool.maomao365.com/twt')
insert into A values(10,'ok')   
select keyId, [info] = stuff((select ',' + [info] from A  t where t.keyId = A.id for xml path('')) , 1 , 1 , '')  
from A  
group by keyId
go
truncate table A 
drop table A