【发布时间】:2020-06-18 13:04:12
【问题描述】:
我不断收到此错误:
Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.
在寻找这个答案的解决方案时,我看到了这篇关于在 SELECT 语句中更新的 SO 文章:How do I UPDATE from a SELECT in SQL Server?
示例...注意:CustomProperties 字段是 nvarchar(max),我将其转换为 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
我也尝试了光标(讨厌的东西),但得到了同样的错误。
有没有一种方法可以对所有行进行全面更新。我的目标是从 CustomProperties XML 中删除此表中特定行数据的一个节点。
【问题讨论】:
-
所以你的数据库是 SQLServer ?
-
是的。让我更新一下问题。
标签: sql sql-server xml tsql xquery