Table Structure
ID Name Dept Salary
1 John IT 1000
2 Jamie HR 2000
3 Ron FN 3000
XML needed as follows
<Data>
<EMP Id=1>
<Name>John</Name>
<Dept>IT</Dept>
<Salary>1000</Salary>
</Emp>
<EMP Id=2>
<Name>Jamie</Name>
<Dept>HR</Dept>
<Salary>2000</Salary>
</Emp>
<EMP Id=3>
<Name>Ron</Name>
<Dept>FN</Dept>
<Salary>3000</Salary>
</Emp>
</Data>
我正在使用以下数据库查询从结果集构建XML -
SELECT * FROM <DBName> FOR XML PATH('EMP'), ROOT ('DATA')
不确定如何获得上述所需的XML,即如上所述的属性“id”
答案 0 :(得分:0)
尝试这样
SELECT ID AS [@Id]
,Name
,Dept
,Salary
FROM YourTable
FOR XML PATH('EMP'), ROOT ('Data')
小心区分大小写。在XML DATA
不等于到Data