我有一个如下表结构:
SELECT
[EmpID], [EmpName],
[DeptName],
[BirthDate]
FROM
[dbo].[Employees]
我想将此表数据转换为 XML,最终输出将如下所示:
I want to convert this table data into XML and the final output will be like below:
<Employees>
<Department DeptName="ABC">
<Employee EmpID="1">
<EmpName>Davolio</EmpName>
<BirthDate>10/12/1989</BirthDate>
</Employee>
<Employee EmpID="2">
<EmpName>Andrew</EmpName>
<BirthDate>05/02/1985</BirthDate>
</Employee>
</Department>
<Department DeptName="DEF">
<Employee EmpID="3">
<EmpName>David</EmpName>
<BirthDate>11/09/1982</BirthDate>
</Employee>
</Department>`enter code here
</Employees>
试试这个
SELECT [DeptName]
,( SELECT [EmpID],
[EmpName],
[BirthDate]
FROM @table E
WHERE E.DeptName = D.DeptName
FOR XML PATH ('Employee'),TYPE
)
FROM @table D
GROUP BY [DEPTNAME]
FOR XML PATH ('Department'),type,ROOT('Employees')
这篇关于如何使用具有多个子节点的sql将表数据转换为xml格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!