我正在摆弄XML AUTO和XML RAW以了解它们的行为方式。测试数据库为AdventureWorks2012
。
SELECT TOP 20 Cust.CustomerID AS C1,
Cust.TerritoryID,
OrderHeader.CustomerID AS C2,
OrderHeader.SalesOrderID,
OrderHeader.Status
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML RAW('MyRow'), ELEMENTS
XML RAW的简单性只是将每个元素放在同一级别
<MyRow>
<C1>29825</C1>
<TerritoryID>5</TerritoryID>
<C2>29825</C2>
<SalesOrderID>43659</SalesOrderID>
<Status>5</Status>
对于XML AUTO
SELECT TOP 20 Cust.CustomerID,
Cust.TerritoryID,
OrderHeader.CustomerID,
OrderHeader.SalesOrderID,
OrderHeader.Status
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML AUTO, ELEMENTS
我有
<Cust>
<CustomerID>29825</CustomerID>
<TerritoryID>5</TerritoryID>
<OrderHeader>
<CustomerID>29825</CustomerID>
<SalesOrderID>43659</SalesOrderID>
<Status>5</Status>
</OrderHeader>
</Cust>
XML AUTO决定将所有Cust
标记分组。如何得出这样的结论:Cust
以某种方式包含在&#34;更高层次&#34;比OrderHeader
。为什么它决定将所有OrderHeader
列列在OrderHeader标记下,而Cust
则不一样?在执行之前我认为它会是
<Cust>
<CustomerID>29825</CustomerID>
<TerritoryID>5</TerritoryID>
</Cust>
<OrderHeader>
<CustomerID>29825</CustomerID>
<SalesOrderID>43659</SalesOrderID>
<Status>5</Status>
</OrderHeader>
更新
这是一种预感,但我开始认为SQL Server会将首先提到的列的表作为外部标记,
例如,
SELECT TOP 20 OrderHeader.CustomerID,,
Cust.CustomerID,
Cust.TerritoryID,
OrderHeader.SalesOrderID,
OrderHeader.Status
即使我混合了列顺序,它仍然会将OrderHeader的列组合在一起,而Cust是一个子组
<OrderHeader>
<CustomerID>29825</CustomerID>
<SalesOrderID>43659</SalesOrderID>
<Status>5</Status>
<Cust>
<CustomerID>29825</CustomerID>
<TerritoryID>5</TerritoryID>
</Cust>
</OrderHeader>