You can use SQL Server's XML.modify( 'replace value of' ... ) to do this. Here is an example that you can run in SSMS that should get you moving in the right direction.
-- SAMPLE DATA --
-- ADDED AN EXTRA CUSTOM COLUMN FOR THIS EXAMPLE --
DECLARE @Data TABLE ( id INT PRIMARY KEY IDENTITY (1,1), Comment VARCHAR(255), MyXmlCol XML );
INSERT INTO @Data ( Comment, MyXmlCol )
VALUES (
'I need to update this comment to Value for "My New Column".',
'<CustomColumns>
<CustomColumn>
<Name>My New Column</Name>
<DataType>0</DataType>
<Value>The data to be updated</Value>
</CustomColumn>
<CustomColumn>
<Name>My Other New Column</Name>
<DataType>0</DataType>
<Value>This value will not change.</Value>
</CustomColumn>
</CustomColumns>'
);
SELECT id, Comment, MyXmlCol FROM @Data;
This initial SELECT returns:
+----+-------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | Comment | MyXmlCol |
+----+-------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | I need to update this comment to Value for "My New Column". | <CustomColumns><CustomColumn><Name>My New Column</Name><DataType>0</DataType><Value>The data to be updated</Value></CustomColumn><CustomColumn><Name>My Other New Column</Name><DataType>0</DataType><Value>This value will not change.</Value></CustomColumn></CustomColumns> |
+----+-------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
The current XML is:
<CustomColumns>
<CustomColumn>
<Name>My New Column</Name>
<DataType>0</DataType>
<Value>The data to be updated</Value>
</CustomColumn>
<CustomColumn>
<Name>My Other New Column</Name>
<DataType>0</DataType>
<Value>This value will not change.</Value>
</CustomColumn>
</CustomColumns>
Next up, update "Comment" to "Value" for "My New Column":
UPDATE @Data
SET
MyXmlCol.modify( 'replace value of (/CustomColumns/CustomColumn[Name="My New Column"]/Value/text())[1] with (sql:column("Comment"))' )
SELECT id, Comment, MyXmlCol FROM @Data;
This new SELECT returns:
+----+-------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | Comment | MyXmlCol |
+----+-------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | I need to update this comment to Value for "My New Column". | <CustomColumns><CustomColumn><Name>My New Column</Name><DataType>0</DataType><Value>I need to update this comment to Value for "My New Column".</Value></CustomColumn><CustomColumn><Name>My Other New Column</Name><DataType>0</DataType><Value>This value will not change.</Value></CustomColumn></CustomColumns> |
+----+-------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
And here is the updated XML:
<CustomColumns>
<CustomColumn>
<Name>My New Column</Name>
<DataType>0</DataType>
<Value>I need to update this comment to Value for "My New Column".</Value>
</CustomColumn>
<CustomColumn>
<Name>My Other New Column</Name>
<DataType>0</DataType>
<Value>This value will not change.</Value>
</CustomColumn>
</CustomColumns>
Couple of things to note about:
replace value of (/CustomColumns/CustomColumn[Name="My New Column"]/Value/text())[1]
- Use "text()" to set the text value of a node.
- The [1] is required as
you can only target 1 node using'replace'.
- Note the use of
CustomColumn[Name="My New Column"]
to restrict the update.
Also note:
Using sql:column("Comment")
allows you to reference another column (in this case [Comment]) in your table without breaking SQL's "string literal" requirement with XML.