SQL SERVER: How to Replace XML Node's Attribute Value in XML Column of Table

Introduction:  In this article i am going to explain how to update the value of XML node's attribute in XML data type column in all or specific row of sql server table.


Implementation: Let create a table variable for demonstration purpose.

DECLARE @Employee TABLE(Id INT IDENTITY(1,1),empXML XML); 

Let's insert some dummy data into this table

INSERT INTO @Employee VALUES
(
            '<ROOT>
            <Employee Id="100"/>
            <Employee Id="101"/>
            <Employee Id="102"/>      
            </ROOT>'
)
INSERT INTO @Employee VALUES
(
            '<ROOT>
            <Employee Id="100"/>
            <Employee Id="106"/>
            <Employee Id="107"/>      
            </ROOT>'
)
INSERT INTO @Employee VALUES
(
            '<ROOT>
            <Employee Id="100"/>
            <Employee Id="102"/>
            <Employee Id="109"/>      
            </ROOT>'
)
INSERT INTO @Employee VALUES
(
            '<ROOT>
            <Employee Id="111"/>
            <Employee Id="107"/>
            <Employee Id="101"/>      
            </ROOT>'
)

Show table data
SELECT * FROM @Employee

Replace XML node's attribute value in XML Column of sql server table


Now suppose we want to update the value of Xml node's attribute value from 101 to 777 in all rows where exists.

So let's declare two variables. @OldEmployeeId is the Id that we want to replace and @NewEmployeeId is the Id that we want to replace with.

DECLARE @OldEmployeeId INT=101, @NewEmployeeId INT=777;

The query will be as:

UPDATE @Employee
SET empXML.modify('replace value of (ROOT/Employee[@Id eq sql:variable("@OldEmployeeId")]/@Id)[1] with sql:variable("@NewEmployeeId")')
WHERE empXML.exist('ROOT/Employee[@Id eq sql:variable("@OldEmployeeId")]') = 1;

Show updated table data

SELECT * FROM @Employee

Result will be as:

Id
empXML
1
<ROOT><Employee Id="100" /><Employee Id="777" /><Employee Id="102" /></ROOT>
2
<ROOT><Employee Id="100" /><Employee Id="106" /><Employee Id="107" /></ROOT>
3
<ROOT><Employee Id="100" /><Employee Id="102" /><Employee Id="109" /></ROOT>
4
<ROOT><Employee Id="111" /><Employee Id="107" /><Employee Id="777" /></ROOT>

Now suppose we want to update the value of Xml node's attribute value from 101 to 777 in specific row i.e. where the value of column Id=4

The query will be as:

UPDATE @Employee
SET empXML.modify('replace value of (ROOT/Employee[@Id eq sql:variable("@OldEmployeeId")]/@Id)[1] with sql:variable("@NewEmployeeId")')
WHERE empXML.exist('ROOT/Employee[@Id eq sql:variable("@OldEmployeeId")]') = 1 AND Id=4;

Show updated table data

SELECT * FROM @Employee

Result will be as:
Id
empXML
4
<ROOT><Employee Id="111" /><Employee Id="107" /><Employee Id="777" /></ROOT>

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..