T-SQL和XML输出

时间:2014-09-28 03:28:28

标签: sql-server xml tsql

所以我已经写了关于连接和左连接等...我正在研究的这个xml项目。但是我在XML部分有一些时间。我可以将所有数据都放入数据集中。但是我无法按照我想要的方式获取数据输出xml。

TransactionTable与transactionID上的TimeEntry表连接。 1对多 TransactionTable内部连接到ClientTable 1到1

数据全部与我的select语句中的左连接和内连接完美结合。所有数据都在那里。

但是我需要它以我需要的XML格式输出。无法弄清楚该怎么做。很近但每次我靠近我都遇到了另一个障碍。

见附图 example

<receivableInvoices>
      <receivableInvoice refId="RECEIVABLEINVOICE-REFID-123">
        <customerCompanyName>Acme Corp</customerCompanyName>
        <customerEmailAddress>jane.doe@acme.com</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>

更新: 这是我正在运行的示例查询

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

4 个答案:

答案 0 :(得分:1)

Yo可以用来在查询后将结果输出到xml使用

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

也可以用于例如

的元素子元素
SELECT .... 
 city AS "billTo/address/city" 
 .... 

我没有您的查询,所以我可以为您发布声明。

用于属性使用

SELECT field_name AS "@refId"  ...

答案 1 :(得分: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;

答案 2 :(得分:1)

试试这个你可能需要通过timeentry

来调整我删除联接的名称或内容
    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

答案 3 :(得分: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')