您可以使用insert (XML DML)修改 XML 变量。
构建要插入到新 XML 变量的 XML,然后使用sql:variable()
.
-- XML to change
declare @XML xml
set @XML =
'<root>
<ati id_ati="16546" />
</root>'
-- Table holding data to insert
declare @T table(id_ati int)
insert into @T values(344),(566),(788),(545)
-- Build the XML you want to add
declare @X xml
set @X =
(
select id_ati as "@id_ati"
from @T
for xml path('ati')
)
-- Modify your XML
set @XML.modify('insert sql:variable("@X") as last into root[1]')
-- View the result
select @XML
@T
用您必须获取数据的任何查询替换表变量。中的 XML@X
如下所示:
<ati id_ati="344" />
<ati id_ati="566" />
<ati id_ati="788" />
<ati id_ati="545" />