Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update XML stored in a XML column in SQL Server

I have a sample table in SQL Server 2012. I am running some queries against but the .modify() XQuery method is executing but not updating.

Here is the table

Xml Table

For this just trying to update settings to 'NewTest'

Xml Snippet

This will execute but nothing is updating! Thanks for any help!

Not Update Snippet

like image 419
KeyboardFriendly Avatar asked May 13 '13 02:05

KeyboardFriendly


2 Answers

Since there is a XML namespace (xmlns:dev="http://www.w3.org/2001/XMLSchema") in your XML document, you must inlcude that in your UPDATE statement!

Try this:

;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema')
UPDATE XmlTable
SET XmlDocument.modify('replace value of (/Doc/@Settings)[1] with "NewTest"')
WHERE XmlId = 1
like image 172
marc_s Avatar answered Nov 04 '22 06:11

marc_s


You should declare a namespace in your update syntax .Try the below syntax

Declare @Sample table
(xmlCol xml)

Insert into @Sample
values
('<dev:Doc xmlns:dev="http://www.w3.org/2001/XMLSchema" 
                       SchemaVersion="0.1" Settings="Testing" Ttile="Ordering">
        <Person id="1">
            <FirstName>Name</FirstName>
        </Person>
      </dev:Doc>')
 Select * from @Sample
 Update @Sample
 SET xmlCol.modify(
                  'declare namespace ns="http://www.w3.org/2001/XMLSchema";
                   replace value of (/ns:Doc/@Settings)[1]
                   with "NewTest"')

 Select * from @Sample
like image 34
praveen Avatar answered Nov 04 '22 07:11

praveen