I'm struggling to modify some SQL generation of XML.
The following sample code generates 3 lines of output, the first 2 are correct, however, I need the 3rd to render out to a root element of xsi:nil=true.
Many thanks.
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