2014-09-28 85 views
0

所以我寫了關於連接和左連接等。對於這個xml項目我正在努力。然而,我對XML部分有些茫然。我可以將所有數據導入數據集。但是我無法按照我想要的方式獲取數據來輸出xml。T-SQL和XML輸出

TransactionTable離開時與交易ID上的TimeEntry表連接。 1到很多 TransactionTable內部連接到ClientTable 1到1

數據都很好地出現在我的select語句的左連接和內連接上。所有數據都在那裏。

但是我需要它以我需要的XML格式輸出。無法弄清楚如何去做。接近了,但每次我接近我碰到另一個路障。

見圖附着以及 example

<receivableInvoices> 
     <receivableInvoice refId="RECEIVABLEINVOICE-REFID-123"> 
     <customerCompanyName>Acme Corp</customerCompanyName> 
     <customerEmailAddress>[email protected]</customerEmailAddress> 
     <invoiceNumber>123456</invoiceNumber> 
     <invoiceDate>2014-05-01</invoiceDate> 
     <billTo> 
      <address> 
      <line1>Acme Corp</line1> 
      <line2>123 Main Street</line2> 
      <line3>STE 100</line3> 
      <line4>Attn: Shipping</line4> 
      <city>Maitland</city> 
      <stateProvince>FL</stateProvince> 
      <postalCode>32751</postalCode> 
      <country>US</country> 
      </address> 
      <contact> 
      <name>Jane Doe</name> 
      <phoneNumber>555-555-5555</phoneNumber> 
      </contact> 
     </billTo> 
     <lineItems> 
      <lineItem> 
      <lineNumber>1</lineNumber> 
      <hours>75.00</hours> 
      <description>Description of the line item goes here.</description> 
      </lineItem> 
     </lineItems> 
     </receivableInvoice> 
<receivableInvoices> 

UPDATE: 該處是我運行示例查詢

SELECT 
      tt.TransactionID as transactionID, 
      c.ClientID as customerRefID, 
      c.ClientCompany as customerCompanyName, 
      c.clientEmail as customerEmailAddress, 
      tt.TransactionInvNum as invoiceNumber, 
      tt.InvoiceDate as invoiceDate, 
      DATEADD(d,30,tt.InvoiceDate) as dueDate, 
      tt.TransactionInvBillAmt as totalAmount, 
      tt.TransactionInvBillAmt as balance, 
      'USD' as currencyCode, 
      'Invoice from Customer X' as description, 
      '30' as terms, 
      right(tt.PRojectID, (LEN(tt.projectid) - charindex(':',tt.projectid))) as purchaseOrderNumber, 
      right(tt.PRojectID, (LEN(tt.projectid) - charindex(':',tt.projectid))) as salesOrderNumber, 
      c.ClientCompany as "shipTo/address/line1", 
      c.ClientStreet as "shipTo/address/line2", 
      c.ClientStreet2 as "shipTo/address/line3", 
      c.ClientCity as "shipTo/address/city", 
      c.ClientState as "shipTo/address/stateProvince", 
      c.ClientZip as "shipTo/address/postalCode", 
      c.ClientFName + ' ' + c.ClientLName as "shipTo/contact/name", 
      c.ClientPhone as "shipTo/contact/phoneNumber", 
      c.ClientCompany as "billTo/address/line1", 
      c.ClientStreet as "billTo/address/line2", 
      c.ClientStreet2 as "billTo/address/line3", 
      c.ClientCity as "billTo/address/city", 
      c.ClientState as "billTo/address/stateProvince", 
      c.ClientZip as "billTo/address/postalCode" 
      FROM TransactionTable tt 
      INNER JOIN Client c 
      ON c.ClientID = tt.ClientID 
      LEFT JOIN timeentry te ON 
      te.TransactionID = tt.transactionID 
      WHERE tt.PayID is null 

     FOR XML PATH('receivableInvoice'), ROOT('receivableInvoices'), ELEMENTS 

輸出鏈路Output of SQL

+0

那麼你想把你的查詢結果放入XML而不是行和列? – 2014-09-28 03:41:49

+0

正確。我可以編寫所有的連接,並且已經閱讀了XML和T-SQL如何一起播放。但我不知道如何打破這個下降 – GrafixMastaMD 2014-09-28 03:46:38

回答

0

感謝每一個一。我已經完成了我所需要的。請看下面我是如何完成的。

SELECT 
a.TransactionID as [@refId], 
a.ClientID as customerRefId, 
a.ClientCompany as customerCompanyName, 
a.ClientEmail as customerEmailAddress, 
a.TransactionInvNum as invoiceNumber, 
cast(a.InvoiceDate as DATE) as invoiceDate, 
cast(a.TransactionInvBillAmt as decimal(8,2)) as totalAmount, 
cast(a.TransactionInvBillAmt as decimal(8,2)) as balance, 
cast(DATEADD(day, COALESCE(a.GraceDays, 0), a.InvoiceDate) as DATE) as dueDate, 
COALESCE(REPLACE(SUBSTRING(a.Country, CHARINDEX('(', a.Country)+1, LEN(RTRIM(a.Country))), ')', ''), 'USD') as currencyCode, 
a.ProjectPhase as purchaseOrderNumber, 
a.ProjectPhase as salesOrderNumber, 

-- BILL TO SECTION (Client Address) 
a.ClientCompany     as "billTo/address/line1", 
a.ClientStreet     as "billTo/address/line2", 
a.ClientStreet2     as "billTo/address/line3", 
'Attn: Shipping'    as "billTo/address/line4", 
a.ClientCity     as "billTo/address/city", 
a.ClientState     as "billTo/address/stateProvince", 
a.ClientZip      as "billTo/address/postalCode", 
a.ClientCountry     as "billTo/address/country", 
a.ClientFName+' '+a.ClientLName as "billTo/address/contact/name", 
LEFT(a.ClientPhone,3) + '-' + SUBSTRING(a.ClientPhone,4,3) + '-' + RIGHT(a.ClientPhone,4) 
           as "billTo/address/contact/phoneNumber", 

-- REMIT TO SECTION (Client Address) 
a.ClientCompany     as "remitTo/address/line1", 
a.ClientStreet     as "remitTo/address/line2", 
a.ClientStreet2     as "remitTo/address/line3", 
'Attn: Shipping'    as "remitTo/address/line4", 
a.ClientCity     as "remitTo/address/city", 
a.ClientState     as "remitTo/address/stateProvince", 
a.ClientZip      as "remitTo/address/postalCode", 
a.ClientCountry     as "remitTo/address/country", 
a.ClientFName+' '+a.ClientLName as "remitTo/address/contact/name", 
LEFT(a.ClientPhone,3) + '-' + SUBSTRING(a.ClientPhone,4,3) + '-' + RIGHT(a.ClientPhone,4) 
           as "remitTo/address/contact/phoneNumber", 

(
    SELECT 
     ct.LineNumber          as lineNumber, 
     cast((ct.TEHours * ct.TEBillRate) as decimal(8,2)) as totalAmount, 
     ct.TEDescription         as description, 
     p.ProjectPhase          as purchaseOrderNumber, 
     ct.LineNumber          as purchaseOrderLineNumber, 
     p.ProjectPhase          as salesOrderNumber, 
     ct.LineNumber          as salesOrderLineNumber, 
     p.ProjectPhase          as vendorItemNumber, 
     p.ClientID           as customerItemNumber, 
     p.ProjectName          as project, 
     0             as taxAmount, 
     cast(ct.TEDate as date)        as taxDate 
    FROM TimeEntry as ct 
    INNER JOIN PRoject p 
     ON p.ProjectID = ct.ProjectID 
    WHERE a.TransactionID = ct.TransactionID 
    ORDER BY ct.LineNumber 
    FOR XML PATH('LineItem'), ROOT('LineItems'), TYPE 
) 

-- Sub Query so that we can get all the line entries from one invoice under the same invoice node 
FROM (
    SELECT 
     tt.InvoiceDate, 
     tt.TransactionID, 
     c.ClientStreet, 
     c.ClientStreet2, 
     c.ClientCity, 
     c.ClientState, 
     c.ClientZip, 
     c.ClientCountry, 
     tt.TransactionInvNum, 
     c.ClientID, 
     c.ClientCompany, 
     c.ClientEmail, 
     t.Name, 
     t.GraceDays, 
     tt.TransactionInvBillAmt, 
     cm.Country, 
     tt.ProjectPO, 
     c.ClientFName, 
     c.ClientLName, 
     c.ClientPhone, 
     p.ProjectPhase 
    FROM TransactionTable tt 
    INNER JOIN Client c 
     ON tt.ClientID = c.ClientID 
    INNER JOIN Project P 
     ON p.ProjectID = tt.ProjectID 
    LEFT JOIN TermsTable t 
     ON tt.TermID = t.TermID 
    LEFT JOIN CurrencyMultiplier cm 
     ON cm.CurrencyID = c.CurrencyID 
    WHERE 
     tt.PayID is null 
     AND tt.InvoiceDate BETWEEN @startDate AND @endDate 
     AND tt.TransactionInvNum LIKE '%' + @invNum + '%' 
) a 

     FOR XML PATH('recievableInvoice'), ROOT('recievableInvoices') 
1

喲可以使用以輸出結果以後查詢到xml使用

SELECT .... FROM TABLE1 INNER JOIN TABLE2 ... 
FOR XML PATH('receivableInvoices') 

也爲元素的孩子,你可以使用例如

SELECT .... 
city AS "billTo/address/city" 
.... 

我沒有你的查詢,所以我可以張貼的聲明爲您服務。

的屬性利用

SELECT field_name AS "@refId" ... 
+0

我得到了這個。但是因爲一張發票有多行(意思是多個時間段),所以它複製了發票節點。雖然它應該只是一個 – GrafixMastaMD 2014-09-28 04:05:16

+0

我提高了這一點,因爲這是一個很好的起點。儘管我自己已經有了這一點。 – GrafixMastaMD 2014-09-28 17:48:48

1

直加盟效率不高在這種情況下,最好使用嵌套FOR XML子查詢:

SELECT 
    tt.TransactionID as transactionID, 
    c.ClientID as customerRefID, (
     select te.Id as [lineNumber], 
      te.Description as [Description] 
     from dbo.timeentry te 
     where te.TransactionID = tt.transactionID 
     for xml path('lineItem'), root('lineItems'), elements, type 
    ) 
FROM dbo.TransactionTable tt 
    INNER JOIN dbo.Client c ON c.ClientID = tt.ClientID 
WHERE tt.PayID is null 
FOR XML PATH('receivableInvoice'), ROOT('receivableInvoices'), ELEMENTS, type; 
+0

擁有大量的數據。如何將正確的時間條目放在正確的調用節點下的子查詢完全獨立?從主查詢到子查詢,我沒有看到明智的領帶關係。 – GrafixMastaMD 2014-09-28 16:27:29

+0

@GrafixMastaMD,它不是獨立的,它是相關的:'where te.TransactionID = tt.transactionID''tt'別名在最外層定義,'te'別名在子查詢中。 – 2014-09-29 06:06:07

1

試試這個,你可能需要tweek名稱或東西我刪除加入時間進入

SELECTtime 
tt.TransactionID as transactionID, 
c.ClientID as customerRefID, 
c.ClientCompany as customerCompanyName, 
c.clientEmail as customerEmailAddress, 
tt.TransactionInvNum as invoiceNumber, 
tt.InvoiceDate as invoiceDate, 
DATEADD(d,30,tt.InvoiceDate) as dueDate, 
tt.TransactionInvBillAmt as totalAmount, 
tt.TransactionInvBillAmt as balance, 
'USD' as currencyCode, 
'Invoice from Customer X' as description, 
'30' as terms, 
right(tt.PRojectID, (LEN(tt.projectid) - charindex(':',tt.projectid))) as purchaseOrderNumber, 
right(tt.PRojectID, (LEN(tt.projectid) - charindex(':',tt.projectid))) as salesOrderNumber, 
c.ClientCompany as "shipTo/address/line1", 
c.ClientStreet as "shipTo/address/line2", 
c.ClientStreet2 as "shipTo/address/line3", 
c.ClientCity as "shipTo/address/city", 
c.ClientState as "shipTo/address/stateProvince", 
c.ClientZip as "shipTo/address/postalCode", 
c.ClientFName + ' ' + c.ClientLName as "shipTo/contact/name", 
c.ClientPhone as "shipTo/contact/phoneNumber", 
c.ClientCompany as "billTo/address/line1", 
c.ClientStreet as "billTo/address/line2", 
c.ClientStreet2 as "billTo/address/line3", 
c.ClientCity as "billTo/address/city", 
c.ClientState as "billTo/address/stateProvince", 
c.ClientZip as "billTo/address/postalCode", 
cast(
    ( 
    Select 
    te.lineNumber, 
    te.hours, 
    te.description 
    From timeentry te 
where te.TransactionID = tt.transactionID 
FOR XML PATH('lineItem'), ROOT('lineItems'), ELEMENTS) as xml).query('.') 

FROM TransactionTable tt 
INNER JOIN Client c 
ON c.ClientID = tt.ClientID 
WHERE tt.PayID is null 

FOR XML PATH('receivableInvoice'), ROOT('receivableInvoices'), ELEMENTS 
+0

我加了這篇文章,只是因爲它幫助我理解了xml類型的嵌套並多查詢它們。 – GrafixMastaMD 2014-09-28 17:47:33