Advertisement
Intermediate SQL Server, XML Data Type & Querying

How do you modify a specific part of an XML document stored in a column without replacing the entire document?

Short answer

You use the modify method on an XML column combined with XML DML statements like insert, replace value of, or delete, specifying an XPath to target the exact node you want to change, letting you make targeted updates to a specific part of a large XML document efficiently.

Save SQL Server mock interview
Advertisement
AModel answer
You use the modify method on an XML column combined with XML DML statements like insert, replace value of, or delete, specifying an XPath to target the exact node you want to change, letting you make targeted updates to a specific part of a large XML document efficiently.
UPDATE ProductCatalog
SET ProductDetails.modify('replace value of (/product/price/text())[1] with "899.99"')
WHERE ProductId = 1;
Real-world example
A product management system updates just the price within a product's XML details using the modify method, leaving the rest of the product's XML structure completely untouched during the targeted update.
Likely follow-ups
  • What other XML DML operations besides replace value of are available with modify?
  • Can you insert an entirely new element into an XML document using modify?
Related topics