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.
In previous articles i explained How to Update multiple xml node values with new values in table and Searching xml node's attribute value exists or not in xml datatype column and Get created or modified date of tables, stored procedures, views and functions and Using case statement inside in clause (alternative) and Select or read xml column data from table
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
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.
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..