【发布时间】:2015-04-28 16:44:02
【问题描述】:
鉴于下面的 T-SQL sn-p 尝试构造 XML。
declare @table table
(
col1 varchar(max),
col2 varchar(max),
col3 xml
)
declare @someXml xml = '
<innerRoot a="b">
<child>1</child>
<child>2</child>
<child>3</child>
</innerRoot>
'
insert into @table values ('VALUE1', 'VALUE2', @someXml)
select
t.col1 as '@attribute1',
t.col2 as '@attribute2',
t.col3 as UnwantedElement
from @table as t
for xml path('Root'), type
生成的 XML 是:
<Root attribute1="VALUE1" attribute2="VALUE2">
<UnwantedElement>
<innerRoot a="b">
<child>1</child>
<child>2</child>
<child>3</child>
</innerRoot>
</UnwantedElement>
</Root>
如何在没有 UnwantedElement 的情况下获得相同的输出,使其看起来像下面的示例。
<Root attribute1="VALUE1" attribute2="VALUE2">
<innerRoot a="b">
<child>1</child>
<child>2</child>
<child>3</child>
</innerRoot>
</Root>
【问题讨论】:
标签: sql-server xml tsql select-for-xml