我努力修改某些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
首先打穀歌我們/庫/ bb510413%28V = sql.120%29.aspx)。第二次谷歌命中:[用XSINIL參數生成NULL值元素](https://msdn.microsoft.com/en-us/library/ms178079%28v=sql.120%29.aspx)。 – GSerg
感謝您花時間回覆,但是,這兩個頁面都不能回答我問的問題。添加XSINIL參數到ELEMENTS指令設置內部元素'IndexDescription'和'Value''xsi:nil =「true」',而不是根元素'ComparativeInvestments' – Jonnie