I have an XML column in SQL Server 2005 with the following simplified XML.
+----------+------------------+
| RecordID | ValueXML |
+----------+------------------+
| 1 | <value>x</value> |
| | |
+----------+------------------+
If I want to change that value to y
, I use the following SQL statement.
update ValueTable
set ValueXML.modify('
replace value of (/value/text())[1]
with ("y")')
where RecordID = 1
That works fine. x
becomes y
. The problem happens if value
is set to nothing.
update ValueTable
set ValueXML.modify('
replace value of (/value/text())[1]
with ("")')
where RecordID = 1
Once this happens, the table looks like this.
+----------+------------------+
| RecordID | ValueXML |
+----------+------------------+
| 1 | <value /> |
| | |
+----------+------------------+
Now, I can no longer set the value. The replace value query executes successfully, but makes no change.
How can I set the text for a closed XML element? Is it possible to "unclose" the element?