0

consider the xml

<root>
  <parent id="1">
    <child>
      <field name="1">
        <value>abc</value>
      </field>
      <field name="2">
        <value>cdf</value>
      </field>
      <field name="2">
        <value>xyz</value>
      </field>
      <field name="1">
         <value>uvw</value>
      </field>
    </child>
  </parent>
  <parent id="2">
    <child>
      <field name="1">
        <value>123</value>
      </field>
      <field name="3">
        <value>234</value>
      </field>
      <field name="4">
        <value>34</value>
      </field>
      <field name="1">
        <value>544</value>
      </field>
   </child>
 </parent>

i want to update the text value of 'value' where 'name' attribute of field node is equal to '1'

here is what i have right now....

update newTable
set xmlcol.modify('replace value of(/root/parent/child/field[@name="1"]/value/text()) with "newValue"')

will this work for all occurrence or just first one???

2
  • 1
    Did you try to test it? Commented Mar 25, 2014 at 19:04
  • i was doing it earlier but could not get the result right now i am on the run so cant test it straight away Commented Mar 25, 2014 at 19:07

2 Answers 2

1

for anyone this worked for me.....

BEGIN

declare @I int

;WITH XMLNAMESPACES(DEFAULT 'your namespace')

SELECT @I= xmlcol.value('count(/root/parent/child/field[@name="1"]/value/)', 'int')

FROM newTable

select @I as a

while @I > 0 

  begin

  ;WITH XMLNAMESPACES(DEFAULT 'your namespace')

  update newTable set xmlcol.modify('replace value of (/root/parent/child/field[@name="1"]/value/text()) with "newValue"')

  set @I = @I - 1

end
end
Sign up to request clarification or add additional context in comments.

Comments

0

This will work only for the first (second or whatever @i is) occurrence.

declare @i int = 1;

update newTable
set xmlcol.modify('replace value of (/root/parent/child/field[@name="1"]/value/text())[{sql:variable("@i")}] with "newValue"');

You should replace all the values with the new ones in a loop.

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.