如何使用 t-sql 更新 xml 变量中所有 xml 属性的值? [英] How to update all xml attributes' value in an xml variable using t-sql?
本文介绍了如何使用 t-sql 更新 xml 变量中所有 xml 属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们有一个示例代码
DECLARE @xml XML = N'
<a abb="122">
<b>
</b>
</a>
<a abb="344">
<b>
</b>
</a>
...
';
SELECT @xml;
--need to update abb to be 888 in @xml here
SELECT @xml;
我们可以一次更新一个属性,如图所示 此处.新问题是:我们如何一次更新属性 abb
的所有出现?
We can update one attribute at a time as showed here. The new question is: How can we update all at-a-time occurences of attribute abb
?
请帮忙.
推荐答案
可以将 XML 拆分为一个表变量,单独替换每个节点,然后再次合并.
You can split the XML to a table variable, replace each node separately and then combine them again.
declare @xml xml =
'<a abb="122">
<b></b>
</a>
<a abb="344">
<b></b>
</a>'
declare @T table (XMLCol xml)
insert into @T
select
a.query('.')
from @xml.nodes('a') a(a)
update @T set
XMLCol.modify('replace value of (/a/@abb)[1] with 888')
set @xml = (select XMLCol as [*]
from @T
for xml path(''))
这篇关于如何使用 t-sql 更新 xml 变量中所有 xml 属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文