【问题标题】:SQL select XML column from table as a xml node (instead of it being nested under another xml node)?SQL 从表中选择 XML 列作为 xml 节点(而不是嵌套在另一个 xml 节点下)?
【发布时间】: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 的&lt;list&gt; 直接嵌套在&lt;MasterList&gt; 下。 IE。无需将其嵌套在 &lt;SecondMasterList&gt;

预期输出:

<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


    【解决方案1】:

    指定属性的路径

    set @outputXML=(SELECT a as 'MasterList/@id', xmlelement as 'MasterList' from #Temp
    FOR XML PATH(''))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2012-04-10
      相关资源
      最近更新 更多