SQL Server - XML子句中的嵌套子元素

时间:2018-02-16 13:07:35

标签: sql-server xml xpath

我使用以下查询来生成XML输出,然后通过存储过程生成XML文件到位置。我创建了一个视图作为存储过程和数据库之间的中间层(View - [DocProcess_Invoice])。

查询

SELECT TOP 1
IDInvoice AS ID,
'FALSE' AS CopyIndicator,
CAST(InvoiceIssueDate AS date) AS IssueDate,
'380' AS InvoiceTypeCode,
DocumentCurrencyCode,
'RON' AS TaxCurrencyCode,
'RON' AS PricingCurrencyCode,
'RON' AS PaymentCurrencyCode,
'RON' AS PaymentAlternativeCurrencyCode,
LineCountNumeric,(
    SELECT 
        (SELECT
            IDOrderReference AS ID,
            IssueDate

            FROM RO_ErpZero.dbo.[DocProcess_Invoice] WHERE GID='1D70DC20-7E7D-4D08-AE3F-1E9466CB7508'
            FOR XML PATH(''), TYPE)

    FROM RO_ErpZero.dbo.[DocProcess_Invoice] 
    FOR XML PATH('OrderReference'), TYPE)

FROM RO_ErpZero.dbo.[DocProcess_Invoice] WHERE GID='1D70DC20-7E7D-4D08-AE3F-1E9466CB7508'
FOR XML PATH('Invoice'))

输出

<Invoice>
<ID>FAV-FDIS180-02436</ID>
<CopyIndicator>FALSE</CopyIndicator>
<IssueDate>2018-02-16</IssueDate>
<InvoiceTypeCode>380</InvoiceTypeCode>
<DocumentCurrencyCode>LEU</DocumentCurrencyCode>
<TaxCurrencyCode>RON</TaxCurrencyCode>
<PricingCurrencyCode>RON</PricingCurrencyCode>
<PaymentCurrencyCode>RON</PaymentCurrencyCode>
<PaymentAlternativeCurrencyCode>RON</PaymentAlternativeCurrencyCode>
<LineCountNumeric>1</LineCountNumeric>
<OrderReference>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
</OrderReference>
<OrderReference>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
</OrderReference>
<OrderReference>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
</OrderReference>
<OrderReference>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
<ID>0</ID>
<IssueDate>1900-01-01</IssueDate>
</OrderReference>
</Invoice>

视图的目的是返回发票号和发票上的行。视图的查询结果是4行,我相信,由于是4行,这就是我在XML输出中第四次使用标记<ID><IssueDate>的原因。

查看结果

Preview here

我要查找的所需输出应该只带<ID><IssueDate> <OrderReference>标记内的<Invoice> <ID>FAV-FDIS180-02436</ID> <CopyIndicator>FALSE</CopyIndicator> <IssueDate>2018-02-16</IssueDate> <InvoiceTypeCode>380</InvoiceTypeCode> <DocumentCurrencyCode>LEU</DocumentCurrencyCode> <TaxCurrencyCode>RON</TaxCurrencyCode> <PricingCurrencyCode>RON</PricingCurrencyCode> <PaymentCurrencyCode>RON</PaymentCurrencyCode> <PaymentAlternativeCurrencyCode>RON</PaymentAlternativeCurrencyCode> <LineCountNumeric>1</LineCountNumeric> <OrderReference> <ID>0</ID> <IssueDate>1900-01-01</IssueDate> </OrderReference> </Invoice> ,因为它与我的订单参考编号相同发票,我肯定错过了一些东西,但我不知道是什么。

所需的输出

intersection_cardinality = len(set.intersection(*[x, y]))

我希望我能以某种方式解释清楚......

1 个答案:

答案 0 :(得分:0)

我真的不明白这一点......如果没有测试场景,很难得到你想要的东西。为什么有很多行,为什么你只需要一行?这些值是否有可能不同,您在XML中需要更多这些值?

如果没有,在您的子选择中使用TOP 1DISTINCT可能就足够了

(SELECT TOP 1 --or DISTINCT
        IDOrderReference AS ID,
        IssueDate

        FROM RO_ErpZero.dbo.[DocProcess_Invoice] WHERE GID='1D70DC20-7E7D-4D08-AE3F-1E9466CB7508'
        FOR XML PATH(''), TYPE)

这会将结果集减少到第一行(或不同的集合)。

如果这对您没有帮助,请使用实际的RDBMS(产品和版本)进行标记,然后阅读How to ask a good SQL questionHow to create a MCVE

乍一看,我很确定,这可以做得更好......