How to update all xml attributes' value in an xml variable using t-sql?

前端 未结 1 1156
南笙
南笙 2021-01-27 17:29

Let\'s have a sample snipet

DECLARE @xml XML = N\'

    
    


    
    

        
相关标签:
1条回答
  • 2021-01-27 18:25

    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(''))
    
    0 讨论(0)
提交回复
热议问题