Table dbo.table_a contains a XML column foo. Every row has a XML-tree in the a XML-column. Secondary table dbo.table_b contains old value old_val and new value new_val
The XML-tree looks like this
<root>
<Customer>
<Name>ACME</Name>
</Customer>
<Def>
<Enabled>true</Enabled>
<CIds>
<Id>ABC</Id>
<Id>DEF</Id>
</CIds>
</Def>
</root>
Please Note that /root/Def/CIds can contain zero or several child nodes called Id.
dbo.table_b looks like this
+---------+---------+
| old_val | new_val |
+---------+---------+
| ABC | 123 |
| DEF | 456 |
+---------+---------+
I need to replace the value for every Id by joining the current xml node value with dbo.table_b on old_val and replacing the xml node value with new_val. ABC should be replace with 123 and DEF with 456
There is no easy way to achieve this, due to the fact that even now, in 2018, the
replace value ofXML method still does not support modification of multiple nodes.The simplest approach is the insert+delete tactic, as in the following example:
Its weakness is that it reconstructs the entire
/CIdsnode, so if you have any additional data in it, like attributes, it might be too cumbersome to recreate. In this case you might have better success with a FLWOR update, but they tend to be quite slow, compared to other options.Alternatively, you might run a series of atomic updates in a loop. As unpleasant as it may sound, it will actually work, especially if your actual XML is much more complex than the provided example.