How can I update an xml node than contains another node with a specific value XML in sql server -


example

declare @x xml;  set @x = n' <root>   <book>      <id>1</id>      <price>100</price>      <title>false</title>   </book>   <book>         <id>2</id>         <price>200</price>         <price>false</price>   </book>   <book>         <id>3</id>         <price>300</price>         <title>false</title>    </book>    <book>         <id>4</id>         <price>400</price>         <title>false</title>    </book> </root>' 

query select node contains child node id =3

select @tmp = a.b.query('../.')  @x.nodes('//book/id[text()="3"]') a(b) 

how can modify price value of selected node?

consider using sql server's xml data modification language (dml), replace value of method. below example updates price 9999:

declare @x xml;  set @x = n' <root>   <book>      <id>1</id>      <price>100</price>      <title>false</title>   </book>   <book>         <id>2</id>         <price>200</price>         <price>false</price>   </book>   <book>         <id>3</id>         <price>300</price>         <title>false</title>    </book>    <book>         <id>4</id>         <price>400</price>         <title>false</title>    </book> </root>'  set @x.modify('replace value of (/root/book[id="3"]/price/text())[1] ("9999")')  select @x 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -