2015-09-01 80 views
0

我努力修改某些SQL生成的XML。從SQL生成XML nil ='true'

下面的示例代碼生成3行輸出,前2個是正確的,但是,我需要第三個渲染出xsi:nil = true的根元素。

非常感謝。

CREATE TABLE #ReportPackComparativeIndices 
(
    ReportPackRequestID INT, 
    IndexDescription VARCHAR(250), 
    Value DECIMAL(18,1) 
) 

INSERT INTO #ReportPackComparativeIndices VALUES (25984, 'ClientIndexID0', 28.3) 
INSERT INTO #ReportPackComparativeIndices VALUES (25984, 'ClientIndexID1', 43.5) 
INSERT INTO #ReportPackComparativeIndices VALUES (25984, 'ClientIndexID2', 81.1) 
INSERT INTO #ReportPackComparativeIndices VALUES (25984, 'ClientIndexID3', 24.5) 
INSERT INTO #ReportPackComparativeIndices VALUES (25985, 'ClientIndexID0', 93.9) 
INSERT INTO #ReportPackComparativeIndices VALUES (25985, 'ClientIndexID1', 53.8) 
INSERT INTO #ReportPackComparativeIndices VALUES (25985, 'ClientIndexID2', 69.3) 
INSERT INTO #ReportPackComparativeIndices VALUES (25985, 'ClientIndexID3', 26.8) 
INSERT INTO #ReportPackComparativeIndices VALUES (25986, NULL, NULL) 

SELECT * FROM #ReportPackComparativeIndices 


-- Render out the XML Fragments 
SELECT ti.ReportPackRequestID, 
CAST(
(
    SELECT 
     ti2.IndexDescription, 
     ti2.Value 
    FROM 
     #ReportPackComparativeIndices AS ti2 
    WHERE 
     ti.ReportPackRequestID = ti2.ReportPackRequestID 
    FOR XML PATH('ComparisonValue'), 
     ROOT('ComparativeInvestments'), 
     ELEMENTS, 
     TYPE 
) AS NVARCHAR(MAX)) AS XmlFragment 
FROM 
    #ReportPackComparativeIndices AS ti 
    GROUP BY 
     ti.ReportPackRequestID 
    ORDER BY 
     ti.ReportPackRequestID 
+0

首先打穀歌我們/庫/ bb510413%28V = sql.120%29.aspx)。第二次谷歌命中:[用XSINIL參數生成NULL值元素](https://msdn.microsoft.com/en-us/library/ms178079%28v=sql.120%29.aspx)。 – GSerg

+0

感謝您花時間回覆,但是,這兩個頁面都不能回答我問的問題。添加XSINIL參數到ELEMENTS指令設置內部元素'IndexDescription'和'Value''xsi:nil =「true」',而不是根元素'ComparativeInvestments' – Jonnie

回答

2

剛將XSINIL添加到您的ELEMENTS中。這是缺少的嗎?

編輯:有了這一招,你可以創建一個XSI:零上一級,如果這兩個元素是空的,但我寧願去想設計...

SELECT ti.ReportPackRequestID, 
CAST(
(
    SELECT 
     CASE WHEN ti2.IndexDescription IS NULL AND ti2.Value IS NULL THEN 'true' ELSE NULL END AS [@xsi:nil], 
     ti2.IndexDescription, 
     ti2.Value 
    FROM 
     #ReportPackComparativeIndices AS ti2 
    WHERE 
     ti.ReportPackRequestID = ti2.ReportPackRequestID 
    FOR XML PATH('ComparisonValue'), 
     ROOT('ComparativeInvestments'), 
     ELEMENTS XSINIL, 
     TYPE 
) AS NVARCHAR(MAX)) AS XmlFragment 
FROM 
    #ReportPackComparativeIndices AS ti 
    GROUP BY 
     ti.ReportPackRequestID 
    ORDER BY 
     ti.ReportPackRequestID; 

還有一個嘗試:

SELECT ti.ReportPackRequestID, 
CAST(
(
    SELECT 
     CASE WHEN ti2.IndexDescription IS NULL AND ti2.Value IS NULL THEN 'true' ELSE NULL END AS [ComparativeInvestments/@xsi:nil], 
     ti2.IndexDescription AS [ComparativeInvestments/ComparisonValue/IndexDescription], 
     ti2.Value AS [ComparativeInvestments/ComparisonValue/Value] 
    FROM 
     #ReportPackComparativeIndices AS ti2 
    WHERE 
     ti.ReportPackRequestID = ti2.ReportPackRequestID 
    FOR XML PATH('dummy'), 
     ELEMENTS XSINIL, 
     TYPE 
) AS NVARCHAR(MAX)) AS XmlFragment 
FROM 
    #ReportPackComparativeIndices AS ti 
    GROUP BY 
     ti.ReportPackRequestID 
    ORDER BY 
     ti.ReportPackRequestID; 
+0

謝謝,但我恐怕不會,這會將IndexDescription和Value設置爲的xsi:零,而不是根元素 'ComparativeInvestments':<值xsi:nil =「true」/> Jonnie

+1

@JonathonRalfe,根元素本身是否應該是NULL?我編輯我的答案,但我覺得有什麼不對的設計... – Shnugo

+0

感謝@Shnugo,這似乎給我一個很好XSI:無=真上ComparisonValue,而不是根元素ComparisonInvestment上。我瞭解到,您對根元素非常不空點,但這個位於這是其他地方縫合在一起的SSIS包內,所以是不是真正的根元素,但我的雙手被縛。 – Jonnie

1

我最終選擇的選項是嵌套兩個select語句,丟失了XMLRoot元素,轉而將兩個單獨的XML PATH語句應用於外部(舊根)元素。

SELECT ti.ReportPackRequestID, 
CAST(
(
    SELECT 
    (
     SELECT 
      CASE WHEN ti2.IndexID IS NOT NULL 
       THEN 'ClientIndexID' + CAST(ti2.RowNumber -1 AS VARCHAR(5)) 
       ELSE NULL 
      END AS IndexID, 
      ti2.IndexTotalReturn AS Value 
     FROM 
      #ReportPackComparativeIndices AS ti2 
     WHERE 
      ti.ReportPackRequestID = ti2.ReportPackRequestID 
     ORDER BY 
      ti2.RowNumber 
     FOR XML PATH('ComparisonValue'), 
      ELEMENTS, 
      TYPE 
    ) 
    FOR XML PATH('ComparativeInvestments'), 
     ELEMENTS XSINIL, 
     TYPE 
) AS NVARCHAR(MAX)) AS XmlFragment 
FROM 
    #ReportPackComparativeIndices AS ti 
GROUP BY 
    ti.ReportPackRequestID 
ORDER BY 
    ti.ReportPackRequestID 

這讓我在XML所需的輸出:[:指定XSINIL與ELEMENTS指令實施例:(https://msdn.microsoft.com/en-

<ComparativeInvestments xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>