Introduction:
In this article I am going to share how
to modify or we can say replace multiple XML node/ attribute values with new
values in single or multiple update statement(s) based on condition in sql server table.
In Previous articles i explained Parse xml and get data as comma separated string format and Convert xml data to sql database table and Delete records from table using inner join in sql server and Get age in years, months and days from date of birth and Query to find all foreign keys references of particular table
Implementation:
Let’s create an employee table using table variable for demonstration purpose and
insert some dummy xml data in xml type column of table.
DECLARE @Employee TABLE(empXML XML)
INSERT INTO @Employee VALUES('<ROOT><Employee
Id="101" Name="Hrithik" City="Noida"
Age="32" /></ROOT>')
INSERT INTO @Employee VALUES('<ROOT><Employee
Id="102" Name="Ranbeer" City="Delhi"
Age="25" /></ROOT>')
INSERT INTO @Employee VALUES('<ROOT><Employee
Id="103" Name="Akshay" City="Mumbai"
Age="56" /></ROOT>')
INSERT INTO @Employee VALUES('<ROOT><Employee
Id="104" Name="Sidharth" City="Kolkata"
Age="33" /></ROOT>')
INSERT INTO @Employee VALUES('<ROOT><Employee
Id="105" Name="Randeep" City="Chennai"
Age="29" /></ROOT>')
INSERT INTO @Employee VALUES('<ROOT><Employee
Id="106" Name="Varun" City="Bengaluru"
Age="30" /></ROOT>')
Check
table data
SELECT * FROM @Employee
Suppose we
want to update value of Employee Id to "111", Name to "Aamir", City to "Chandigarh" and Age to "40" where Employee Id is 104
Then there
are two options as following:
First is
to update each node based on condition as
UPDATE @Employee SET
empXML.modify('replace value of (ROOT/Employee/@Name)[1] with
"Aamir"')
WHERE empXML.value('(ROOT/Employee/@Id)[1]',
'INT') = 104
UPDATE @Employee SET
empXML.modify('replace value of (ROOT/Employee/@City)[1] with
"Chandigarh"')
WHERE empXML.value('(ROOT/Employee/@Id)[1]',
'INT') = 104
UPDATE @Employee SET
empXML.modify('replace value of (ROOT/Employee/@Age)[1] with
"40"')
WHERE empXML.value('(ROOT/Employee/@Id)[1]',
'INT') = 104
UPDATE @Employee SET
empXML.modify('replace value of (ROOT/Employee/@Id)[1] with
"111"')
WHERE empXML.value('(ROOT/Employee/@Id)[1]',
'INT') = 104
Second and
Best way:
DECLARE @xmlData xml;
SELECT @xmlData = empXML FROM
@Employee;
SET @xmlData.modify('replace value of
(ROOT/Employee/@Id)[1] with "111"')
SET @xmlData.modify('replace value of
(ROOT/Employee/@Name)[1] with "Aamir"')
SET @xmlData.modify('replace value of
(ROOT/Employee/@City)[1] with "Chandigarh"')
SET @xmlData.modify('replace value of
(ROOT/Employee/@Age)[1] with "40"')
-- This way we only need
to do one update on the table instead of multiple updates corresponding to each
node/attribute we wish to update.
UPDATE @Employee
SET empXML = @xmlData WHERE empXML.value('(ROOT/Employee/@Id)[1]', 'INT') = 104
Check
updated table data now
SELECT * FROM @Employee
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..