我努力修改某些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