使用 SQL 服务器 XML 数据类型,如何在使用另一个属性搜索以定位 xml 节点时更新值属性?

With SQL Server XML data type, how to update the value attribute when searching with another attribute to locate the xml node?

我的 table 中有一个 XML 列,它存储这样一个 XML 结构:

<cars>
    <car name="Ford" value="123" />
    <car name="Honda" value="456" />
</cars>

我想修改名称="Honda" 和值=“456”的所有行。我需要将号码 456 更新为 789。

我试过这个:

UPDATE cars
SET XmlData.modify('replace value of (/cars/car[@name="Honda"]/value) with "789"')
WHERE isProcessed = 0

我收到错误:

Msg 2261, Level 16, State 1, Line 41
XQuery Cars.XmlData.modify()]: There is no element named 'value' in the type 'element(car,#anonymous) *'.

你是如此接近......只是错过了.../@value)[1]

例子

Declare @Cars table (ID int,IsProcessed bit,XMLData xml)
Insert Into @Cars values
(1,0,'<cars><car name="Ford" value="123" /><car name="Honda" value="456" /></cars>')

Update @Cars
 Set   XMLData.modify('replace value of (/cars/car[@name="Honda"]/@value)[1]  with "789"')
 WHERE isProcessed = 0

Select * from @Cars

更新后XML

<cars>
  <car name="Ford" value="123" />
  <car name="Honda" value="789" />
</cars>