Example: Specify the ELEMENT directive
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
This retrieves employee information and generates element-centric XML as shown in the following:
<Employee EmpID=...>
<Name>
<FName>...</FName>
<LName>...</LName>
</Name>
</Employee>
The query remains the same, except you add the ELEMENT
directive in the column names. Therefore, instead of attributes, the <FName>
and <LName>
element children are added to the <Name>
element. Because the Employee!1!EmpID
column doesn't specify the ELEMENT
directive, EmpID
is added as the attribute of the <Employee>
element.
SELECT 1 as Tag,
NULL as Parent,
E.BusinessEntityID as [Employee!1!EmpID],
NULL as [Name!2!FName!ELEMENT],
NULL as [Name!2!LName!ELEMENT]
FROM HumanResources.Employee AS E
INNER JOIN Person.Person AS P
ON E.BusinessEntityID = P.BusinessEntityID
UNION ALL
SELECT 2 as Tag,
1 as Parent,
E.BusinessEntityID,
FirstName,
LastName
FROM HumanResources.Employee AS E
INNER JOIN Person.Person AS P
ON E.BusinessEntityID = P.BusinessEntityID
ORDER BY [Employee!1!EmpID],[Name!2!FName!ELEMENT]
FOR XML EXPLICIT;
This is the partial result.
<Employee EmpID="1">
<Name>
<FName>Ken</FName>
<LName>Sánchez</LName>
</Name>
</Employee>
<Employee EmpID="2">
<Name>
<FName>Terri</FName>
<LName>Duffy</LName>
</Name>
</Employee>
...
See also
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho