sql2005中一个xml聚合的例子

该问题来自论坛提问,演示SQL代码如下

  1. --建立测试环境
  2. setnocounton
  3. createtabletest(IDvarchar(20),NAMEvarchar(20))
  4. insertintotestselect'1','aaa'
  5. insertintotestselect'1','bbb'
  6. insertintotestselect'1','ccc'
  7. insertintotestselect'2','ddd'
  8. insertintotestselect'2','eee'
  9. go
  10. --测试
  11. select*from(selectdistinctidfromtest)a
  12. OUTERAPPLY(
  13. selectvalue='<root>'+(
  14. selectnamefromtestpath
  15. whereid=A.id
  16. forxmlauto)+'<root/>')b
  17. --删除测试环境
  18. droptabletest
  19. setnocountoff
  20. /*--结果
  21. idvalue
  22. ----------------------------------------------------------------------------------------
  23. 1<root><pathname="aaa"/><pathname="bbb"/><pathname="ccc"/><root/>
  24. 2<root><pathname="ddd"/><pathname="eee"/><root/>
  25. */

你可能感兴趣的:(sql,xml,Go)