我正在努力修改一些 SQL 生成的 XML。
以下示例代码生成 3 行输出,前 2 行是正确的,但是,我需要将第 3 行渲染到 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