![](/img/trans.png)
[英]Update xml node attributes in SQL Server, filtering on other attribute
[英]With SQL Server XML data type, how to update the value attribute when searching with another attribute to locate the xml node?
我的表中有一個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,第16級,狀態1,第41行
XQuery Cars.XmlData.modify()]:類型為'element(car,#anonymous)*'的元素中沒有名為'value'的元素。
您是如此親密...只是錯過了.../@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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.