【发布时间】:2016-05-18 15:41:17
【问题描述】:
DECLARE @outputXML xml
CREATE TABLE #Temp (a varchar(128), xmlelement xml)
INSERT INTO #Temp values('1', N'<list id="1"/><list id="2"/>')
INSERT INTO #Temp values('3', N'<list id="3"/><list id="4"/>')
set @outputXML=(SELECT a as '@id', xmlelement as 'SecondMasterList' from #Temp
FOR XML PATH('MasterList'))
select @outputXML
DROP TABLE #Temp
上面的脚本@outputXML 抛出下面的XML
<MasterList id="1">
<SecondMasterList><list id="1" /><list id="2" /></SecondMasterList>
</MasterList>
<MasterList id="3">
<SecondMasterList><list id="3" /><list id="4" /></SecondMasterList>
</MasterList>
必填:
但我需要@outputXML 的<list> 直接嵌套在<MasterList> 下。 IE。无需将其嵌套在 <SecondMasterList>
预期输出:
<MasterList id="1">
<list id="1" /><list id="2" />
</MasterList>
<MasterList id="3">
<list id="3" /><list id="4" />
</MasterList>
让我知道应如何修改查询以获得预期的输出。 非常感谢任何帮助!
【问题讨论】:
标签: sql-server xml stored-procedures xpath