2

I have a table with the following format:

int IndexField
xml ConfigField

A ConfigField field value might look similar to

 <Parameters>
      <Parameter Type="Config" Name="Prefix">hd</Parameter>
      <Parameter Type="Config" Name="PgNumber">2</Parameter>
      <Parameter Type="Config" Name="IsValid">False</Parameter>
    </Parameters>

I would like to know the SQL Server syntax to modifythe Parameters XML node for the PgNumber value from 2 to 3 where IndexField = 22

Thanks

2 Answers 2

1

This is one possible XQuery expression to be passed to the SQL Server's modify() method for updating value of <Parameter Name='PgNumber'> element :

UPDATE MyTable
SET ConfigField.modify('
        replace value of 
            (/Parameters/Parameter[@Name="PgNumber"]/text())[1] 
        with 3
    ')
WHERE IndexField = 22

Sqlfiddle Demo

Notice that in XPath/XQuery you need to put @ at the beginning of attribute name to reference an XML attribute.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use function modify() and correct XQuery to set the value that you need. The syntax is quite tricky though. In your case you should be able to use such query:

UPDATE YourTableName
SET ConfigField.modify('replace value of (/Parameters/Pararameter[@Name="PgNumber"]/text())[1] with "3"')
WHERE IndexField = 22

4 Comments

I ran that and the message said 1 row affected but when i select again it does not look as if the PgNumber was changed
not sure if this matters but I should add that I am using sql 2012. thanks so much for any help
@jvoigt Sorry, it was my typo - I missed @ symbol, which is required for attribute values. I updated the answer, it should be working now
Yes that was the issue. That worked between your answer and har07 reminder of the @

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.