默认情况下包含 Null 值的列
默认情况下,列中的 Null 值映射为“缺少相应的属性、节点或元素”。通过使用 ELEMENTS 指令请求以元素为中心的 XML 并指定 XSINIL 来请求为 NULL 值添加元素,可以覆盖此默认行为,如以下查询所示:
USE AdventureWorks2008R2;
GO
SELECT E.BusinessEntityID as "@EmpID",
FirstName as "EmpName/First",
MiddleName as "EmpName/Middle",
LastName as "EmpName/Last"
FROM HumanResources.Employee AS E
INNER JOIN Person.Person AS P
ON E.BusinessEntityID = P.BusinessEntitytID
WHERE E.EmployeeID=1
FOR XML PATH, ELEMENTS XSINIL;
下面显示了结果。请注意,如果未指定 XSINIL,将缺少 <Middle> 元素。
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" EmpID="1">
<EmpName>
<First>Ken</First>
<Middle xsi:nil="true" />
<Last>Sánchez</Last>
</EmpName>
</row>