用于生成XML的SQL代码

时间:2016-06-27 10:58:00

标签: sql sql-server xml tsql sql-server-2014

我不熟悉在SQL中编写XML,并且迫切需要一些帮助来为以下xml输出编写SQL。我相信你们其中一个人会发现它真的很容易!任何帮助非常感谢!

<?xml version="1.0"?>
<PaymentList>
<Payment>
<PaymentTypeField>BACS_Payment</PaymentTypeField>
<DebitSortCodeField>300009</DebitSortCodeField>
<DebitAccountNumberField>01163208</DebitAccountNumberField>
<PaymentCurrencyField>GBP</PaymentCurrencyField>
<ValueDateField>17-JUN-2016</ValueDateField>
<DebitAccountNameField>TEST LTD</DebitAccountNameField>
<BeneficiaryList>
<BeneficiaryItem PaymentReferenceNumber="BACS170616" Name="RT & BS LANE"Amount="5443.64" SortCode="202400" AccountNumber="30305146"/>
<BeneficiaryItem PaymentReferenceNumber="BACS170616" Name="CANNON HYGIENE LTD" Amount="197.14" SortCode="400250" AccountNumber="81001884"/>
<BeneficiaryItem PaymentReferenceNumber="BACS170616" Name="COMPLETE OFFICE SO" Amount="102.00" SortCode="406135" AccountNumber="03006513”/>
</BeneficiaryList>
</Payment>
</PaymentList>

由于 保罗..

1 个答案:

答案 0 :(得分:0)

除了事实之外,你的XML有一些缺陷(日期格式和&在&#34; RT&amp; BS LANE&#34;中没有转义字符,除了你没有显示的事实您的数据来源,这将是一个完全返回您的示例的查询:

SELECT 'BACS_Payment' AS PaymentTypeField
      ,30009 AS DebitSortCodeField
      ,'01163208' AS DebitAccountNumberField
      ,'GBP' AS PaymentCurrencyField
      ,'2016-06-17' AS ValueDateField --ATTENTION: Avoid culture specific formats!!!
      ,'TEST LTD' AS DebitAccountNameField
      ,(
        SELECT *
        FROM
        (
         SELECT 
                'BACS170616' AS [@PaymentReferenceNumber]
               ,'RT & BS LANE' AS [@Name]
               ,5443.64 AS [@Amount]
               ,'202400' AS [@SortCode]
               ,'30305146' AS [@AccountNumber]

         UNION ALL SELECT 'BACS170616','CANNON HYGIENE LTD',197.14,'400250','81001884'
         UNION ALL SELECT 'BACS170616','COMPLETE OFFICE SO',102.00,'406135','03006513'

        ) AS subselect
        FOR XML PATH('BeneficiaryItem'),ROOT('BeneficiaryList'),TYPE
       )
FOR XML PATH('Paymnent'),ROOT('PaymentList');

注意

中间的UNION ALL只是一个模拟,用于1:n相关数据表 - 可能 - 是BeneficiaryItem列表的来源。您可以使用正确的WHERE子句进行简单的选择,以读取与外部查询相关的数据。其余的应该非常接近你的需求......

这是结果

<PaymentList>
  <Paymnent>
    <PaymentTypeField>BACS_Payment</PaymentTypeField>
    <DebitSortCodeField>30009</DebitSortCodeField>
    <DebitAccountNumberField>01163208</DebitAccountNumberField>
    <PaymentCurrencyField>GBP</PaymentCurrencyField>
    <ValueDateField>2016-06-17</ValueDateField>
    <DebitAccountNameField>TEST LTD</DebitAccountNameField>
    <BeneficiaryList>
      <BeneficiaryItem PaymentReferenceNumber="BACS170616" Name="RT &amp; BS LANE" Amount="5443.64" SortCode="202400" AccountNumber="30305146" />
      <BeneficiaryItem PaymentReferenceNumber="BACS170616" Name="CANNON HYGIENE LTD" Amount="197.14" SortCode="400250" AccountNumber="81001884" />
      <BeneficiaryItem PaymentReferenceNumber="BACS170616" Name="COMPLETE OFFICE SO" Amount="102.00" SortCode="406135" AccountNumber="03006513" />
    </BeneficiaryList>
  </Paymnent>
</PaymentList>