I have a table in SQL Server 14.0, which is called Users .There are multiple users and each of the user has a column data called as PermissionData which is in XML format. The following is the XML :
<Policy>
<Permissions>
<Item>
<Key>Hello</Key>
</Item>
<Item>
<Key>Bye</Key>
</Item>
</Permissions>
</Policy>
So I want to replace all the Bye with GoodBye value. Some of the users may have this Bye value or may not have. Some other users can have this Bye value as the first key or the second key or the third key. So due to that I cant just do [2] at every key because the order can be different.
The end-result should be like this:
<Policy>
<Permissions>
<Item>
<Key>Hello</Key>
</Item>
<Item>
<Key>GoodBye</Key>
</Item>
</Permissions>
</Policy>
Can anyone help me to write an XQuery that updates the Key value for all the users in the Users table? The following is my attempt to write the query, but it is not working.
UPDATE Users
SET PermissionData.modify('replace value of (/Policy/Permissions/Item/Key)
with( if((/Policy/Permissions/Item/Key/text())="Bye")
then "GoodBye"
else ()
)')