0

I have example XML in one of the table column:

<Root>
   <A>
      <C>c</C>
      <D>d</D>
   </A>
   <B>b</B>
</Root>

How can I replace value of A node into something like that:

<Root>
   <A>
      <E>e</E>
      <F>f</F>
   </A>
   <B>b</B>
</Root>

I tried the following solution

DECLARE @var varchar(100);
SET @var = '<E>e</E><F>f</F>'
SET @xml.modify('replace value of (/Root/A/text())[1] with sql:variable("@var")');

but it did not work...

regards

1
  • If this is SQL-Server, then it's not an XQuery Update question but XML Data Modification Language (XML DML) Commented Mar 14, 2011 at 13:47

1 Answer 1

1

I do not think it is possible to use replace to replace nodes, only values. You can use delete and then insert instead.

declare @xml as xml = '
<Root>
  <A>
    <C>c</C>
    <D>d</D>
  </A>
  <B>b</B>
</Root>'

declare @var xml
set @var = '<E>e</E><F>f</F>'

set @xml.modify('delete /Root/A/*')
set @xml.modify('insert sql:variable("@var") into (/Root/A)[1]')

select @xml
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.