获取包含XML的表的XML

时间:2016-08-03 12:33:05

标签: sql-server xml

我的表@t_myTable包含这样的XML

------
1. <xml><.....></xml>
2. <xml><.....></xml>
3. <xml><.....></xml>
------

现在,我想要的是拥有一个包含所有这些XML的XML。 我试图用这个查询来解决这个问题

SELECT myTable.Value
FROM @t_myTable myTable
FOR XML AUTO, ROOT ('XML'))

但我得到的是一个包含很多嵌套节点的XML,比如我们

<XML>                  <- the XML root as I want
  <myTable>            <- the name of the Table, useless in my XML
    <Value>            <- the name of the column, useless in my XML
      <xml>            <- the xml that I want to append to ROOT Node
        ....           <- ecc. ecc.

所以我想问的是如何以最简单的方式最小化xml中的节点:

<XML>
  <xml>
  .......
  </xml>
  <xml>
  .......
  </xml>
<XML>

???提前感谢您的支持

1 个答案:

答案 0 :(得分:3)

尝试这样

DECLARE @tbl TABLE(xml XML);
INSERT INTO @tbl VALUES
 ('<xml><a>Some a value</a></xml>')
,('<xml><a>Another a value</a></xml>')
,('<xml><a>And one more a value</a></xml>');

SELECT xml AS [*]
FROM  @tbl
FOR XML PATH(''),ROOT('xml')

结果

<xml>
  <xml>
    <a>Some a value</a>
  </xml>
  <xml>
    <a>Another a value</a>
  </xml>
  <xml>
    <a>And one more a value</a>
  </xml>
</xml>