0

我正在阅读一本 SQL 基础教科书,下面是 T-SQL 代码:

SELECT Customer.custid AS [@custid],
Customer.companyname AS [companyname]
FROM Sales.Customers AS Customer
WHERE Customer.custid <= 2
ORDER BY Customer.custid
FOR XML PATH ('Customer'), ROOT('Customers');

谁能告诉我使用 PATH 的意义,以及为什么在 custid 之前有一个 @?

欢迎任何建议,谢谢

4

1 回答 1

2

@列标记为 XML 属性而不是元素 - 即:您将得到

<Customers>
    <Customer custid="1234">
        <companyname>Acme Sprockets Inc</companyname>
    </Customer>
</Customers>

如果你@拿走,你会得到

<Customers>
    <Customer>
        <custid>1234</custid>
        <companyname>Acme Sprockets Inc</companyname>
    </Customer>
</Customers>

Path 允许您自定义 XML 结构,而无需求助于FOR XML EXPLICIT

我将从在线文档开始:http ://technet.microsoft.com/en-us/library/ms189885.aspx

于 2013-09-05T21:59:45.620 回答