I have an XML valued column that has multiple “parent” nodes. I need to insert a “child” node into each parent node. If I use
UPDATE mytable SET mycolumn.modify('insert <child/> into (//parent)[1]')
, then I’m inserting a child node into the first parent only.
How do I insert a child in every parent with a single query?
I believe the easiest way to accomplish this is to reconstruct the XML with a SELECT statement using CROSS APPLY and FOR XML.
Here’s an example, hope it makes sense. With this approach, you could also JOIN to another table for the data you want to insert into the
<child2 />node making this solution quite flexible.