for xml path的应用
create table tb(UserID int,RoleName nvarchar(10))
insert into tb select 1,'超级管理员'
insert into tb select 1,'管理员'
insert into tb select 2,'管理员'
go
select UserID,stuff((select ','+RoleName from tb where UserID=a.UserID for xml path('')),1,1,'')RoleName from tb a group by UserID
go
drop table tb
/*
UserID RoleName
----------- --------------------------------
1 超级管理员,管理员
2 管理员
(2 行受影响)
*/
insert into tb select 1,'超级管理员'
insert into tb select 1,'管理员'
insert into tb select 2,'管理员'
go
select UserID,stuff((select ','+RoleName from tb where UserID=a.UserID for xml path('')),1,1,'')RoleName from tb a group by UserID
go
drop table tb
/*
UserID RoleName
----------- --------------------------------
1 超级管理员,管理员
2 管理员
(2 行受影响)
*/