【问题标题】:SQL Server 2014 - FOR XML AUTO avoid automatic node nestingSQL Server 2014 - FOR XML AUTO 避免自动节点嵌套
【发布时间】:2017-07-17 13:51:22
【问题描述】:

我正在尝试构建一些查询来以 XML 格式导出数据并且我构建了这个查询:

select
    [invoice].*,
    [rows].*,
    [payment].payerID,
    [items].picture
from InvoicesHeader [invoice]
join InvoicesRows   [rows]      on  [rows].invoiceID=[invoice].invoiceID 
join Payments       [payments]  on  [payments].paymentID=[invoice].paymentID
join Items          [items]     on  [items].itemID=[rows].itemID
FOR XML Auto, ROOT ('invoices'), ELEMENTS

我得到了这样的结果

<invoices>    
    <invoice>
        <ID>82</ID>
        <DocType>R</DocType>
        <DocYear>2017</DocYear>
        <DocNumber>71</DocNumber>
        <IssueDate>2017-07-17T15:17:30.237</IssueDate>
        <OrderID>235489738019</OrderID>
        ...
        <payments>
            <payerID>3234423f33</payerID>
            <rows>
                <ID>163</ID>
                <ItemID>235489738019</ItemID>
                <Quantity>2</Quantity>
                <Price>1</Price>
                <VATCode>22</VATCode>
                <Color>-</Color>
                <Size></Size>
                <SerialNumber></SerialNumber>
                <items>
                    <picture>http://nl.imgbb.com/AAOSwOdpXyB4I.JPG</picture>
                </items>
            </rows>
            ....

        </payments>
    </invoice>
</invoices>

虽然我想在哪里有这样的东西

[rows] 是发票的子节点,而不是付款的子节点

<invoices>    
    <invoice>
        <ID>82</ID>
        <DocType>R</DocType>
        <DocYear>2017</DocYear>
        <DocNumber>71</DocNumber>
        <IssueDate>2017-07-17T15:17:30.237</IssueDate>
        <OrderID>235489738019</OrderID>
        ...
        <payments>
            <payerID>3234423f33</payerID>
        </payments>
        <rows>
            <ID>163</ID>
            <ItemID>235489738019</ItemID>
            <Quantity>2</Quantity>
            <Price>1</Price>
            <VATCode>22</VATCode>
            <Color>-</Color>
            <Size></Size>
            <SerialNumber></SerialNumber>
            <items>
                <picture>http://nl.imgbb.com/AAOSwOdpXyB4I.JPG</picture>
            </items>
        </rows>
            ....
    </invoice>
</invoices>

看到一些解决方案有很多

对于 XML 自动

全部放在一起,但是这里的数据来自连接表,很遗憾重新查询2-3次相同的值

如何实现?

谢谢

【问题讨论】:

    标签: sql-server select-for-xml


    【解决方案1】:

    尝试将选择顺序更改为此;

    select
        [invoice].*,
        [payment].payerID,
        [items].picture,
        [rows].*
    
    from InvoicesHeader [invoice]
    join InvoicesRows   [rows]      on  [rows].invoiceID=[invoice].invoiceID 
    join Payments       [payments]  on  [payments].paymentID=[invoice].paymentID
    join Items          [items]     on  [items].itemID=[rows].itemID
    FOR XML Auto, ROOT ('invoices'), ELEMENTS
    

    【讨论】:

    • 已经测试过了,如果我把付款放在第一位,发票就会成为付款的子节点......
    【解决方案2】:

    好吧,发现必须改用FOR XML PATH,并将另一个表添加为子查询,每个FOR XML PATH如下:

    select
    [invoice].*,
    p.payerID,
    (select r.* from InvoiceRows r where r.invoiceID=i.invoiceID for XML PATH ('rows'), type)
    from InvoicesHeader i
    join payment        p on i.paymentID=p.paymentID
    FOR XML PATH('invoice'), ROOT ('invoices'), ELEMENTS
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2019-12-28
      • 2017-08-27
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多