I keep getting this error:
Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.
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.
<CustomProperties
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CustomProperty>
<Dev>....</Dev>
<Key>FieldA</Key>
<Value>....</Value>
</CustomProperty>
<CustomProperty>
<Dev>....</Dev>
<Key>FieldB</Key>
<Value>....</Value>
</CustomProperty>
<CustomProperty>
<Dev>....</Dev>
<Key>FieldC</Key>
<Value>....</Value>
</CustomProperty>
<CustomProperty>
<Dev>....</Dev>
<Key>FieldD</Key>
<Value>....</Value>
</CustomProperty>
</CustomProperties>
DECLARE @myVar varchar(50) = 'FieldA';
UPDATE Table_1
SET
Table_1.CustomProperties = CONVERT(xml, Table_2.CustomProperties).modify('delete (/CustomProperties/CustomProperty[Key = sql:variable("@myVar")])')
FROM
[dbo].MyTable AS Table_1
INNER JOIN [dbo].MyTable AS Table_2 on Table_1.id = Table_1.id
WHERE
// shortened for brevity
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.