在 SQL Server 2008 R2 中,我有一个表,其中包含有效 XML 节点行(一列,xml 类型),如下所示:
create table #t (x xml not null);
insert into #t
values ('<service><value1>stuff</value1><value2>more stuff</value2></service>')
, ('<service><value1>I am a different row</value1><value2>more stuff</value2></service>');
我想将它们选择到一个 XML blob 中:
<services>
<service>. . .</service>
<service>. . .</service>
</services>
我使用 FOR XML PATH 来尝试此操作,但它将列标题“x”作为节点嵌入“服务”节点周围。
select x
from #t
for XML PATH(''), ROOT('services')
这会产生:
<services>
<x><service>. . .</service></x>
<x><service>. . .</service></x>
</services>
如何去掉“x”节点?
我尝试选择 x 作为“”并选择 x 作为“。”但这些是保留字,查询会出错。
最佳答案
这个怎么样?
select x.query('*')
from #t
for xml path(''), root('services')
关于sql-server - 在 SQL Server FOR XML 中选择没有列标题的 xml 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155624/