2017-08-20 68 views
2

我有一個表:TSQL - FOR XML數據添加到根節點

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> 
... 

非常接近,但我需要將數據存入根元素(Payments)。表ClientID中的列需要作爲ClientID屬性包含在根元素中。 ClientID將在表中的所有行上保持一致。

我已經探索了幾種方法,但我沒有得到格式要求。

感謝 通潤

回答

3

我勸你不要使用固定寬度的字符列,而使用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> 
+0

嗨@TT,從我認爲,這有可能是下面的''幾個''節點......而你的問題。當然,'CHAR'應該被替換爲'VARCHAR'。或者可以使用'RTRIM()'。 – Shnugo

+0

@Shnugo啊,是的,這可能會給錯誤的XML,謝謝你指出。 –

+0

@Shnugo我從不同的方式開始,用子查詢選擇Payment元素....只是看到Payment元素也獲得了名稱空間聲明。有時,SQL Server中的XML處理非常混亂。 –