在SQL Server存储过程中使用FOR XML PATH无法生成正确的XML格式

时间:2014-06-09 14:15:30

标签: sql-server xml

我是XML的新手,我正在努力了解如何编写SQL Server存储过程以生成下表中的XML文件。

Contract,ContractSection,ClaimReference,TransactionReference,ClaimReference,Ccy,PTT_Ind,PTT_Fees**
P000013-140, P000013-140_001, C000011-14, CLP00000036, C000011-14, GBP, 50, 100
P000066-888, P000066-888_001,   C000031-28, CLP00000041, C000011-14, GBP, 75, 200

我尝试使用FOR XML PATH生成所需的格式,但未成功,如下所示:

<ReportingContractEntry>
    <Contract>
        <ReinsRef>P000013-140 </ReinsRef>
    <Contract>
    <ReportingSectionEntry>
        <SectionRef>P000013-140_001 </ SectionRef >
    </ReportingSectionEntry>
    <ReportingTransactionEntry>
<ReportingTransactionAmountEntry>
    <EntryReference>CLP00000036</EntryReference>
        <TechAccountAmtItem Type="ptt_ind">
            <Amt Share="reinsurer_share" Ccy="GBP">20.00</Amt>
    </TechAccountAmtItem>
        <TechAccountAmtItem Type="ptt_fees">
            <Amt Share="reinsurer_share" Ccy="GBP">590.00</Amt>
    </TechAccountAmtItem>
</ReportingTransactionAmountEntry>
</ReportingTransactionEntry>
</ReportingContractEntry>
<ReportingContractEntry>
    <Contract>
        <ReinsRef>P000066-888 </ReinsRef>
    <Contract>
    <ReportingSectionEntry>
        <SectionRef>P000013-140_001 </ SectionRef >
    </ReportingSectionEntry>
    <ReportingTransactionEntry>
<ReportingTransactionAmountEntry>
    <EntryReference>CLP00000041</EntryReference>
        <TechAccountAmtItem Type=" ptt_ind ">
            <Amt Share="reinsurer_share" Ccy="GBP">75.00</Amt>
    </TechAccountAmtItem>
        <TechAccountAmtItem Type="ptt_fees">
            <Amt Share="reinsurer_share" Ccy="GBP">200.00</Amt>
    </TechAccountAmtItem>
</ReportingTransactionAmountEntry>
</ReportingTransactionEntry>
</ReportingContractEntry>

到目前为止,我的最大努力是这样,但这仅适用于每TechAccountAmtItemReportingTransactionEntry。我需要添加多个TechAccountAmtItems

SELECT  
   RTRIM(policyRef) AS 'Contract/ReinsRef',
   RTRIM(policyRef) + '_001' AS 'ReportingSectionEntry/SectionRef',
   RTRIM(TransactionReference) AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/EntryReference' ,
   'ptt_ind' AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/@Type',
   'reinsurer_share' AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/Amt/@Share'  ,
   CASE WHEN currency_id = 26 THEN 'GBP' ELSE 'EUR' END AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/Amt/@Ccy'  ,
   SUM(Paid_This_Time_Indemnity) AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/Amt'
   --,SUM(Paid_This_Time_Fees) -- I can't figure out how to get a second <TechAmountItem> within the same <ReportingTransactionAmountEntry> ???????
FROM 
    #tmpClm2
GROUP BY
    policyRef, ClaimReference, TransactionReference, ClaimReference, currency_id
FOR XML PATH ('ReportingContractEntry')

正如我所说,XML对我来说是全新的,所以任何建议都会受到高度赞赏。

我甚至不确定使用FOR XML PATH是否是正确的方法。

它似乎是最简单,最灵活的选择。

1 个答案:

答案 0 :(得分:0)

示例中的架构与您的查询不对应,但问题非常简单。在查询中,您可以使用要返回多个元素的子查询。

而不是:

   'ptt_ind' AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/@Type',
    'reinsurer_share' AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/Amt/@Share'  ,
    CASE WHEN currency_id = 26 THEN 'GBP' ELSE 'EUR' END AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/Amt/@Ccy'  ,
    SUM(Paid_This_Time_Indemnity) AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry/TechAccountAmtItem/Amt'

尝试:

(
    SELECT * FROM
    (
        SELECT
        'ptt_ind' AS 'TechAccountAmtItem/@Type',
        'reinsurer_share' AS 'TechAccountAmtItem/Amt/@Share'  ,
        CASE WHEN currency_id = 26 THEN 'GBP' ELSE 'EUR' END AS 'TechAccountAmtItem/Amt/@Ccy'  ,
        SUM(Paid_This_Time_Indemnity) AS 'TechAccountAmtItem/Amt'
        UNION
        SELECT
        'ptt_fees' AS 'TechAccountAmtItem/@Type',
        'reinsurer_share' AS 'TechAccountAmtItem/Amt/@Share'  ,
        CASE WHEN currency_id = 26 THEN 'GBP' ELSE 'EUR' END AS 'TechAccountAmtItem/Amt/@Ccy'  ,
        SUM(Paid_This_Time_Fees) AS 'TechAccountAmtItem/Amt'
    ) iq FOR XML PATH(''), TYPE
) AS 'ReportingTransactionEntry/ReportingTransactionAmountEntry'