TSQL - FOR XML将数据添加到根节点

时间:2017-08-20 16:55:40

标签: sql-server xml tsql

我有一张桌子:

CREATE TABLE [dbo].[xdOthPay]
(
    AccountNo char(10) NOT NULL,
    PaymentDoneOn datetime NULL,
    PaymentDoneBy char(30) NULL,
    InvoiceNumber char(10) NULL,
    AmountPaid decimal(12,3) NULL,
    Comments char(254) NULL,
    ClientID char(50) NULL,
    InstallID char(50) NULL,
    BatchID char(14) NOT NULL 
)

我需要定期通过SSIS和SFTP导出。

导出的接收者指定以下格式:

<?xml version="1.0" encoding="utf-8"?>
<Payments ClientId="10865" xmlns="http://someUrl.com/core/xml/payment">
    <Payment>
        <AccountNo>VALUE</AccountNo>
        <PaymentDoneOn>VALUE</PaymentDoneOn>
        <PaymentDoneBy>VALUE</PaymentDoneBy>
        <InvoiceNumber>VALUE</InvoiceNumber>
        <AmountPaid>VALUE</AmountPaid>
        <Comments>VALUE</Comments>
    </Payment>
</Payments>

这是我的T-SQL声明:

SELECT 
    AccountNo,
    PaymentDoneOn,
    PaymentDoneBy,
    InvoiceNumber,
    AmountPaid,
    Comments
FROM 
    xdMOBOthPay
FOR XML PATH ('Payment'), ROOT ('Payments'), ELEMENTS

这导致:

<Payments>
    <Payment>
        <AccountNo> 972140</AccountNo>
        <PaymentDoneOn>2017-06-08T00:00:00</PaymentDoneOn>
        <PaymentDoneBy>R Entry BA13177 </PaymentDoneBy>
        <InvoiceNumber> 3804</InvoiceNumber>
        <AmountPaid>468.000</AmountPaid>
        <Comments>Non-Onl Payment </Comments>
    </Payment>
...

哪个非常接近,但我需要将数据输入Root元素(付款)。表ClientID中的列需要作为ClientID属性包含在根元素中。 ClientID将在表的所有行上保持一致。

我已经探索了几种方法,但我没有按要求格式化。

由于 都灵

1 个答案:

答案 0 :(得分:4)

我建议你不要使用固定宽度的char列,而应使用varchar列。该示例添加了命名空间,并使XML略有不同,以便根据需要获取所有内容。该示例针对一个特定ClientID执行此操作,我想这就是您所需要的。

结果有一个不幸的事情,命名空间也应用于Payment元素。如果这是不可接受的,那么有一种解决方法,您将在没有XML命名空间的情况下生成,并且您将应用字符串函数来插入命名空间属性。

CREATE TABLE #xdOthPay(
    AccountNo char(10) NOT NULL,
    PaymentDoneOn datetime NULL,
    PaymentDoneBy char(30) NULL,
    InvoiceNumber char(10) NULL,
    AmountPaid decimal(12,3) NULL,
    Comments char(254) NULL,
    ClientID char(50) NULL,
    InstallID char(50) NULL,
    BatchID char(14) NOT NULL );

INSERT INTO #xdOthPay(AccountNo,AmountPaid,BatchID,ClientID,Comments,InstallID,InvoiceNumber,PaymentDoneBy,PaymentDoneOn)
VALUES(972140,468,'bath-id',10865,'comments','install-id','invoicenr','paymentdoneby','20170101'),
      (972141,468,'bath-id',10865,'comments','install-id','invoicenr','paymentdoneby','20170101');

WITH XMLNAMESPACES(DEFAULT 'http://someUrl.com/core/xml/payment')
SELECT 
    10865 AS '@clientId',
    (
        SELECT 
            AccountNo,
            PaymentDoneOn,
            PaymentDoneBy,
            InvoiceNumber,
            AmountPaid,
            Comments
        FROM 
            #xdOthPay AS i
        WHERE
            i.ClientID=10865
        FOR 
            XML PATH ('Payment'), TYPE
    )
FOR 
    XML PATH ('Payments'), ELEMENTS;

DROP TABLE #xdOthPay;

结果:

<Payments xmlns="http://someUrl.com/core/xml/payment" clientId="10865">
  <Payment xmlns="http://someUrl.com/core/xml/payment">
    <AccountNo>972140    </AccountNo>
    <PaymentDoneOn>2017-01-01T00:00:00</PaymentDoneOn>
    <PaymentDoneBy>paymentdoneby                 </PaymentDoneBy>
    <InvoiceNumber>invoicenr </InvoiceNumber>
    <AmountPaid>468.000</AmountPaid>
    <Comments>comments                                                                                                                                                                                                                                                      </Comments>
  </Payment>
  <Payment xmlns="http://someUrl.com/core/xml/payment">
    <AccountNo>972141    </AccountNo>
    <PaymentDoneOn>2017-01-01T00:00:00</PaymentDoneOn>
    <PaymentDoneBy>paymentdoneby                 </PaymentDoneBy>
    <InvoiceNumber>invoicenr </InvoiceNumber>
    <AmountPaid>468.000</AmountPaid>
    <Comments>comments                                                                                                                                                                                                                                                      </Comments>
  </Payment>
</Payments>