1

我正在尝试编写一个 SP 以返回 XML 格式的发票详细信息以传输给第三方。

我有一个可用的 SP,但它有点乱(下面简化了):

SELECT ( 
    SELECT GETDATE() AS HEADER_SLAStartTime
         , DATEADD(HOUR, @SLA_HOURS, GETDATE()) AS HEADER_SLAEndTime
    FOR XML PATH ('Header'), TYPE

    ) , (

        SELECT ACCT AS CustomerCode
             , ACCTNAME As CustomerName
             , ADDR#1 As AddressLine1
             , ADDR#2 AS AddressLine2
             , ADDR#3 AS AddressLine3
             , ADDR#4 AS AddressLine4
             , POSTCODE AS AddressPostcode
             , TELNO AS AddressTelno

        FROM InvHdr

        WHERE INVNO = @INVNO

        FOR XML PATH('Customer'), TYPE

    ) , (

        SELECT (
            SELECT INVNO AS InvoiceNo
                 , [DATE] AS InvoiceDate
                 , [INVTYPE] AS InvoiceType
                 , CASE [SOURCE] WHEN 0 THEN 'Contract' WHEN 1 THEN 'Manual' WHEN 2 THEN 'Sales Order' ELSE '' END AS InvoiceSourceText
                 , THEIRREF AS CustomerReference
                 , YOURREF AS InternalReference
                 , (
                    SELECT ITEMNO AS ItemCode
                         , [ITEMDESC#1] AS ItemDesc
                         , [TYPE] AS ItemType
                         , [MEMO] AS ItemMemo
                         , [GOODS] AS ItemCharge
                         , [DISCOUNT] AS ItemDiscount

                    FROM InvItems

                    WHERE INVNO = HDR.INVNO

                    FOR XML PATH('InvItem'), TYPE
                    )

            FROM InvHdr HDR

            WHERE INVNO = @INVNO

            FOR XML PATH('InvoiceHeader'), TYPE

        ) , (

            SELECT HDR.[GOODS] AS InvoiceNet
                 , HDR.VAT AS InvoiceVAT
                 , HDR.[GOODS] + HDR.VAT AS InvoiceGross
                 , (

                    SELECT VATCODE AS VATListCode
                         , VATAMT AS VATListAmount
                         , VATDESC AS VATListDescription
                         , VATRATE AS VATListRate
                         , VATGOODS AS VATListGoods

                    FROM InvVAT

                    WHERE InvVAT.INVNO = HDR.INVNO

                    ORDER BY VATAMT DESC

                    FOR XML PATH('VATSummary'), TYPE
                   )
            FROM InvHdr HDR

            WHERE INVNO = @INVNO

            FOR XML PATH('InvoiceFooter'), TYPE
        )
        FOR XML PATH('Invoices'), TYPE


    )
FOR XML PATH(''), ROOT('Output')

这个过程有效,但我必须创建很多这样才能以不同的顺序获取不同的信息,我尝试创建单独的 SP 来分段获取数据,下面是我的第一部分 SP:

CREATE PROCEDURE UDEF_DC_XML_INVOICEFOOTER(
    @INVNO INT
    )

AS

BEGIN


    SELECT HDR.[GOODS] AS InvoiceNet
         , HDR.VAT AS InvoiceVAT
         , HDR.[GOODS] + HDR.VAT AS InvoiceGross
         , (

            SELECT VATCODE AS VATListCode
                 , VATAMT AS VATListAmount
                 , VATDESC AS VATListDescription
                 , VATRATE AS VATListRate
                 , VATGOODS AS VATListGoods

            FROM InvVAT

            WHERE InvVAT.INVNO = HDR.INVNO

            ORDER BY VATAMT DESC

            FOR XML PATH('VATSummary'), TYPE
           )
    FROM InvHdr HDR

    WHERE INVNO = @INVNO

    FOR XML PATH('InvoiceFooter'), TYPE


END

当我尝试调用这个时:

SELECT UDEF_DC_XML_INVOICEFOOTER(@INVNO)
FOR XML PATH('Invoices'), TYPE

我得到错误:

Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.UDEF_DC_XML_INVOICEFOOTER", or the name is ambiguous.

最后,我希望能够创建多个 4/5 线 SP,它们将以正确的顺序调用所有部分。通过按顺序调用各个 SP 或将每个部分写入变量并随后构建完整的 XML。

是否可以在单个语句中调用多个返回 XML 的存储过程?

4

1 回答 1

3

是否可以在单个语句中调用多个返回 XML 的存储过程?

不,但您可以使用返回 XML 的函数。

create function dbo.GetXML(@Value int) returns xml
as
begin
  return (
         select @Value as X
         for xml path('Y'), type
         )
end

像这样使用:

select dbo.GetXML(1)
for xml path('Z')

结果:

<Z>
  <Y>
    <X>1</X>
  </Y>
</Z>
于 2013-05-16T10:47:58.153 回答