Skip to content
Advertisement

SQL: Delete XML node in an UPDATE statement

I keep getting this error:

In search of a solution to this answer I came across this SO article about updating in a SELECT statement: How do I UPDATE from a SELECT in SQL Server?

Example… Note: the field CustomProperties is nvarchar(max) which I convert to xml.

I also tried a cursor (nasty things), but getting the same error.

Is there a way where I can do a blanket update of all rows. My goal is to remove one node from the CustomProperties XML for specific row data in this table.

Advertisement

Answer

You were told already, that it is a very bad idea to store XML in a string type. It is always the best choice to use the appropriate type in your data design.

Besides this, if you have to stick to this design (sometimes we must do quirky things), you might try something along this:

–Creating a mockup-table to simulate your issue:

–your serach string

–the query

The idea in short:

Instead of XMLDML (via .modify()) we use a simple UPDATE ... SET ... and assign a re-created XML. The XML method .query() will return a XML without the one <CustomProperty> matching the predicate.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement