sql server数据表转换成xml

运用 for xml

但是SQL2000对此支持不好,用SQL2005比较好

例如下表

sno       sname                     ssex

08020001  李勇                 男

08020002  刘晨                 女

08020003  王敏                 女

08020004  张立                 男  

 

SQL语句:

select rtrim(sno) as "@no",rtrim(sname) as "@name",rtrim(ssex) as "@sex"

from student

for xml path('student'),type,root('allStudents')

 

结果为:

<allStudents>

  <student no="08020001" name="李勇" sex="男" />

  <student no="08020002" name="刘晨" sex="女" />

  <student no="08020003" name="王敏" sex="女" />

  <student no="08020004" name="张立" sex="男" />

</allStudents>
原文地址:https://www.cnblogs.com/goto/p/2716146.html