2016-01-20 65 views
0

我有一個平坦的表是這樣的:如何在SQL Server中將平臺轉換爲層次結構XML?

DIV_ID DIV_NAME MARKET_ID MARKET_NAME TRANSIT_ID TRANS_NAME 

D1  "D1 NAME" M1  "M1 NAME" T1  "T1 NAME" 
D1  "D1 NAME" M1  "M1 NAME" T2  "T2 NAME" 

想將它與層次結構轉換爲XML,一個不重複的父母:

<DIVISIONS> 
<DIVISION DIV_ID="D1" DIV_NAME="D1 NAME"> 
    <MARKET MARKET_ID="M1" MARKET_NAME="M1 NAME"> 
    <TRANSIT TRANSIT_ID="T1" TRANSIT_NAME="T1 NAME"> 
    <TRANSIT TRANSIT_ID="T1" TRANSIT_NAME="T1 NAME"> 
    </MARKET> 
    <MARKET MARKET_ID="M2" MARKET_NAME="M2 NAME"> 
    ... 
    </MARKET> 
    </DIVISION> 
<DIVISION DIV_ID="" DIV_NAME=""> 
... 
</DIVISION> 
</DIVISIONS> 

看來SQL可以通過直接生成它「FOR XML「與一些參數。我嘗試了一些,但仍然無法得到我想要的。

任何幫助表示讚賞。謝謝!

編輯:我發現了一個解決方案,但SQL Server輸出'& lt;'和'& gt;'而不是嵌套元素中的「<」,「>」。這裏是我的代碼:

SELECT DISTINCT DIV_ID AS '@DIV_ID', DIV_NAME AS '@DIV_NAME', 
    (SELECT DISTINCT MARKET_ID AS '@MARKET_ID', MARKET_NAME AS '@MARKET_NAME', 
    (SELECT TRANSIT_ID AS '@TRANSIT_ID', TRANSIT_NAME AS '@TRANSIT_NAME' 
    FROM HX_DIVISIONS T3 
    WHERE T3.MARKET_ID = T2.MARKET_ID 
    FOR XML PATH ('TRANSIT') 
    ) TRANSITS 
    FROM HX_DIVISIONS T2 
    WHERE T2.DIV_ID = T1.DIV_ID 
    FOR XML PATH ('MARKET')) MARKETS 
FROM HX_DIVISIONS T1 
FOR XML PATH('DIVISION'), ROOT('DIVISIONS') 

我試圖用REPLACE(XXX '& LT;', '<')的另一篇文章中建議,但它不工作。

輸出是這樣的:

<DIVISIONS> 
    <DIVISION DIV_ID="D1" DIV_NAME="D1 NAME"> 
    <MARKETS>&lt;MARKET MARKET_ID="M1" MARKET_NAME="M1 NAME"&gt;&amp;lt;TRANSITS&gt;&lt;TRANSIT TRANSIT_ID="T1" TRANSIT_NAME="T1 NAME"/&gt; 
.... 
</DIVISIONS> 
+0

編輯你的問題,顯示你有什麼到目前爲止已經試過。 –

+0

您的解決方案非常接近...您需要在FOR XML PATH後添加「,TYPE」才能將結果作爲XML獲取,而不是作爲嵌套的文本。但是,這樣做,DISTINCT將無法工作......在我的回答中,我首先使用CTE來獲取DISTINCT DIV_ID值並從那裏嵌套。而你最內在的SELECT(對於transits必須包含DIV_ID在... WHERE ... – Shnugo

+0

嗨,我看到你剛剛在... ...這仍然是開放的嗎?你需要任何進一步的幫助嗎? – Shnugo

回答

3

試試這樣說:

DECLARE @tbl TABLE(DIV_ID VARCHAR(10), DIV_NAME VARCHAR(10), TRANSIT_ID VARCHAR(10), TRANSIT_NAME VARCHAR(10), MARKET_ID VARCHAR(10), MARKET_NAME VARCHAR(10)); 
INSERT INTO @tbl VALUES 
('D1','D1 NAME','T1','T1 NAME','M1','M1 NAME') 
,('D1','D1 NAME','T1','T1 NAME','M2','M2 NAME'); 

WITH DistinctDIVIDs AS 
(
    SELECT DISTINCT DIV_ID,DIV_NAME 
    FROM @tbl 
) 
SELECT DIVs.DIV_ID AS [@DIV_ID] 
     ,DIVs.DIV_NAME AS [@DIV_NAME] 
     ,(
      SELECT markets.MARKET_ID AS [@MARKET_ID] 
       ,markets.MARKET_NAME AS [@MARKET_NAME] 
       ,(
        SELECT transits.TRANSIT_ID AS [@TRANSIT_ID] 
          ,transits.TRANSIT_NAME AS [@TRANSIT_NAME] 
        FROM @tbl AS transits 
        WHERE transits.DIV_ID=DIVs.DIV_ID AND transits.MARKET_ID=markets.MARKET_ID 
        FOR XML PATH('TRANSIT'),TYPE 
       ) 
      FROM @tbl AS markets 
      WHERE markets.DIV_ID=DIVs.DIV_ID 
      FOR XML PATH('MARKET'),TYPE 
     ) 
FROM DistinctDIVIDs AS DIVs 
FOR XML PATH('DIVISION'),ROOT('DIVISIONS') 

結果

<DIVISIONS> 
    <DIVISION DIV_ID="D1" DIV_NAME="D1 NAME"> 
    <MARKET MARKET_ID="M1" MARKET_NAME="M1 NAME"> 
     <TRANSIT TRANSIT_ID="T1" TRANSIT_NAME="T1 NAME" /> 
    </MARKET> 
    <MARKET MARKET_ID="M2" MARKET_NAME="M2 NAME"> 
     <TRANSIT TRANSIT_ID="T1" TRANSIT_NAME="T1 NAME" /> 
    </MARKET> 
    </DIVISION> 
</DIVISIONS> 
相關問題