0

Let's have a sample snipet

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;

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?

Please help.

1 Answer 1

3

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(''))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.