Update XML node value in SQL Server

后端 未结 2 1521
半阙折子戏
半阙折子戏 2020-12-13 04:48

I need to update the GroupID field to a 0. I have figured out how to retrieve the value, I am now running into problems updating it.

Any help would ge great!

相关标签:
2条回答
  • 2020-12-13 05:01

    You can do something like this:

    UPDATE 
       dbo.profiles
    SET 
       ProfileXML.modify('replace value of (/ProblemProfile/GroupID/text())[1] with "0"')
    WHERE
       id = 23
    

    Check out this article on SQL Server 2005 XQuery and XML-DML for more details on what you can do with the .modify keyword (insert, delete, replace etc.).

    Marc

    PS: In order to get the value, it would be a lot easier to do just this:

    SELECT ProfileXML.value('(/ProblemProfile/GroupID)[1]', 'int') as ID
    FROM dbo.profiles
    WHERE id = 23
    

    (unless of course you need the XML as a SQL variable for something else later on)

    0 讨论(0)
  • 2020-12-13 05:14

    The simplest way to change the text inside element

    UPDATE [TableName]
       SET  
          [fieldName].modify('replace value of (root/elementName/text())[1] with "wBob"')
    GO
    
    0 讨论(0)
提交回复
热议问题