Consider the following code to update "/project/cost/final" in an untyped XML.
The code will run successfully, however, the value actually remains unchanged.
DECLARE @xml XML;
SET @xml = '
<project>
<cost>
<budget>100</budget>
<estimated>92</estimated>
<final />
</cost>
</project>
';
SET @xml.modify('
replace value of (/project[1]/cost[1]/final[1]/text()[1]) with 95
');
SELECT @xml;
I understand that the "<value name="final" />" element has no text node at the moment, so there is nothing to replace.
So I should insert text node instead, and it will work:
SET @xml.modify('
insert text{"95"} into (/project[1]/cost[1]/final[1])
');
But I need to first check if the text node exists, and it brings a lot of complication to my program.
How can I update the value without checking/inserting "text"?
Note: I am working with untyped XML.