--建立测试环境
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=’<root>’+(
select name from test path
where id = A.id
for xml auto)+’<root/>’) b
--删除测试环境
drop table test
set nocount off
/*--结果
id value
--------------------
1 <root><path name="aaa"/><path name="bbb"/><path name="ccc"/><root/>
2 <root><path name="ddd"/><path name="eee"/><root/>
*/
第一考试网友情提示:如果您遇到任何疑问,请登录第一考试网考试辅导频道或添加qq:,第一考试网以“为考友服务”为宗旨,秉承“快乐学习,轻松考试!”的理念,旨在为广大考友打造一个良好、温馨的学习与交流平台,欢迎持续关注。以上是小编为大家推荐的《计算机等级考试辅导:sql2005中一个xml聚合的例子》相关信息。
编辑推荐