且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 t-sql 更新 xml 变量中所有 xml 属性的值?

更新时间:2022-03-26 06:58:55

可以将 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(''))