使用SQL FOR XML PATH,如何实现以下格式化XML输出?

时间:2015-03-26 03:34:09

标签: sql sql-server sql-server-2012 for-xml for-xml-path

这个项目的范围比这个问题大得多。我已经完成了一个项目的任务,而且我不会给你带来私密的细节。最终我需要做的是将数据从数据库中提取到XML中,这样我就可以转换为JSON并创建一个简单的Web应用程序,这样我就能够以满足客户需求的方式解析和格式化数据。 #39;需要。

我确信有更好的方法可以做到这一点,但这是我已经解决的道路..

我有大约46,000条记录被转储到临时表中。为了让您了解这些数据是如何构建的,运行以下查询:

SELECT 
    TransactionID,
    OwnerID,  
    Date, 
    TransactionType,
    ChargeCode, 
    Description, 
    DebitAmount
FROM #OwnerHistoryTemp
WHERE OwnerID = '11111111'

返回:

TransactionID   OwnerID     Date        TransactionType ChargeCode  Description         DebitAmount
28727           11111111    2014-12-01  E               A1          APPLY CHARGES       210.00  
28728           11111111    2014-12-03  C               A1          DB11111111          210.00  
28729           11111111    2015-01-01  E               A1          APPLY CHARGES       183.37  

我在这里要做的是使用SQL FOR XML PATH(对任何其他建议开放)输出数据,如下所示:

<OwnerHistory>
    <OwnerID OwnerID="11111111">
        <Transactions>
            <TransactionID ID="28727">
                <Date>2014-12-01</Date>
                <TransactionType>E</TransactionType>
                <ChargeCode>A1</ChargeCode>
                <Description>APPLY CHARGES</Description>
                <DebitAmount>210.00</DebitAmount>
            </TransactionID>
            <TransactionID ID="28728">
                <Date>2014-12-03</Date>
                <TransactionType>C</TransactionType>
                <ChargeCode>A1</ChargeCode>
                <Description>DB11111111</Description>
                <DebitAmount>210.00</DebitAmount>
            </TransactionID>
            <TransactionID ID="28729">
                <Date>2015-1-01</Date>
                <TransactionType>E</TransactionType>
                <ChargeCode>A1</ChargeCode>
                <Description>APPLY CHARGES</Description>
                <DebitAmount>183.37</DebitAmount>
            </TransactionID>
        </Transactions>
    </OwnerID>
</OwnerHistory>

我的查询,让我接近,但不是那里的QUITE。因为同一OwnerID出现多次(每个TransactionID一次),运行以下查询:

SELECT 
    OwnerID AS "@OwnerID", 
    TransactionID AS "Transaction/@RecordID", 
    Date AS "Transaction/Date", 
    TransactionType AS "Transaction/TransactionType",
    ChargeCode AS "Transaction/ChargeCode", 
    Description AS "Transaction/Description", 
    DebitAmount AS "Transaction/DebitAmount"
FROM #OwnerHistoryTemp
WHERE OwnerID = '11111111'
GROUP BY OwnerID, RecordID, Date, ChargeCode, Description, DebitAmount
order by OwnerID
FOR XML PATH ('OwnerID'), ROOT('OwnerHistory')

返回以下内容:

<OwnerHistory>
    <OwnerID OwnerID="11111111">
        <Transaction RecordID="28727">
            <Date>2014-12-01</Date>
            <TransactionType>E</TransactionType>
            <ChargeCode>A1</ChargeCode>
            <Description>APPLY CHARGES</Description>
            <DebitAmount>210.0000</DebitAmount>
        </Transaction>
    </OwnerID>
    <OwnerID OwnerID="11111111">
        <Transaction RecordID="28728">
            <Date>2014-12-03</Date>
            <TransactionType>C</TransactionType>
            <ChargeCode>A1</ChargeCode>
            <Description>DB11111111</Description>
            <DebitAmount>210.0000</DebitAmount>
        </Transaction>
    </OwnerID>
    <OwnerID OwnerID="11111111">
        <Transaction RecordID="28729">
            <Date>2015-01-01</Date>
            <TransactionType>E</TransactionType>
            <ChargeCode>A1</ChargeCode>
            <Description>APPLY CHARGES</Description>
            <DebitAmount>183.3700</DebitAmount>
        </Transaction>
    </OwnerID>
</OwnerHistory>

有关如何仅将OwnerID作为Parent拉出并将其下面的所有交易分组的任何想法?

可能是简单的事我只是不理解,或者可能是不可能的。如果前者是这样的话,请随意公开告诉我......

1 个答案:

答案 0 :(得分:3)

准备好你的鞭子?

嵌套查询以获取嵌套XML,如下所示:

SELECT TOP 1
  OwnerID AS "@ID",
  (SELECT
      TransactionID AS "Transaction/@ID",
      [Date] AS "Transaction/Date",
      TransactionType AS "Transaction/Type",
      ChargeCode AS "Transaction/ChargeCode",
      [Description] AS "Transaction/Description",
      DebitAmount AS "Transaction/DebitAmount"
    FROM OwnerHistory
    WHERE OwnerID = [Owner].OwnerID
    FOR XML PATH(''), TYPE) Transactions
FROM OwnerHistory [Owner]
WHERE OwnerID = '11111111' 
FOR XML PATH('Owner'), ROOT('OwnerHistory'), TYPE

产生的XML:

<OwnerHistory>
  <Owner ID="11111111">
    <Transactions>
      <Transaction ID="28727">
        <Date>2015-03-26</Date>
        <Type>E</Type>
        <ChargeCode>A1</ChargeCode>
        <Description>APPLY CHARGES</Description>
        <DebitAmount>210.0000</DebitAmount>
      </Transaction>
      <Transaction ID="28728">
        <Date>2015-03-26</Date>
        <Type>C</Type>
        <ChargeCode>A1</ChargeCode>
        <Description>DB11111111</Description>
        <DebitAmount>210.0000</DebitAmount>
      </Transaction>
      <Transaction ID="28729">
        <Date>2015-03-26</Date>
        <Type>E</Type>
        <ChargeCode>A1</ChargeCode>
        <Description>APPLY CHARGES</Description>
        <DebitAmount>183.3700</DebitAmount>
      </Transaction>
    </Transactions>
  </Owner>
</OwnerHistory>

请注意,仅包括TOP 1以避免为包含OwnerID的每一行重复整个事务集。这可以通过多种方式处理;通常这种嵌套是两个规范化表连接的结果,因此只会出现Owner的一个实例。

MSDN有一些很好的examples来演示这种技术。