Update UnTyped XML data column in SQL Server 2008/2005

Saturday, August 8, 2009 |

Now a day, XML is booming, it is welcomed from every platform so why Microsoft and especially SQL Server sit back? After SQL Server 2005, Microsoft have provided so many functionality for make developer’s life easy who are using XML. Today, I would like you to show how you can update un-typed XML data from within well known UPDATE T-SQL statement.

--create table for testing purpose with XML column
--in SQL Server 2005 or in 2008
create table xmlTest
ColumnXML xml
--insert data in XML column
insert into xmlTest
  <ClientName>Ritesh Shah</ClientName>  
  <Product>HP Notebook</Product>  
--check the output
select * from xmlTest
--update value (un-typed) in product tag
DECLARE @Val varchar(50) 
SELECT @Val = 'Dell Inspiron' 
UPDATE xmlTest 
SET ColumnXML.modify('replace value of (/Orders/Product/text())[1] with sql:variable("@Val")') 
--confirm changes
select * from xmlTest

Reference: Ritesh Shah
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of http://www.SQLHub.com


Anonymous said...

Fantastic post !