--建立測試環(huán)境
set nocount on
create table test(ID varchar(20),NAME varchar(20))
insert into test select ’1’,’aaa’
insert into test select ’1’,’bbb’
insert into test select ’1’,’ccc’
insert into test select ’2’,’ddd’
insert into test select ’2’,’eee’
go
--測試
select *from (select distinct id from test)a
OUTER APPLY(
select value=’’+(
select name from test path
where id = A.id
for xml auto)+’ ’) b
--刪除測試環(huán)境
drop table test
set nocount off
/*--結果
id value
--------------------
1
2
*/
set nocount on
create table test(ID varchar(20),NAME varchar(20))
insert into test select ’1’,’aaa’
insert into test select ’1’,’bbb’
insert into test select ’1’,’ccc’
insert into test select ’2’,’ddd’
insert into test select ’2’,’eee’
go
--測試
select *from (select distinct id from test)a
OUTER APPLY(
select value=’
select name from test path
where id = A.id
for xml auto)+’
--刪除測試環(huán)境
drop table test
set nocount off
/*--結果
id value
--------------------
1
2
*/