我需要更新SQL Server列中的XML节点。
我看到了许多使用xpath并使用属性标识节点的示例。
我正在使用的XML是自动生成的,并且没有在我需要的节点上附带属性。我如何构造update语句来使用节点中的另一个值来查找它?
我希望能够写出这样的东西:
declare @xml xml ='
<Content>
<ContentItems>
<ContentItem>
<ContentKey>abc</ContentKey>
<Body>TextToUpdate</Body>
</ContentItem>
<ContentItem>
<ContentKey>efg</ContentKey>
<Body>Other</Body>
</ContentItem>
</ContentItems>
</Content>'
select @xml
set @xml.modify(
'
replace value of
(/content/contentitems[ContentKey="abc"]/body/text())[1]
with ("Success")')
select @xml发布于 2015-12-05 11:57:42
你的方向是对的。只有错误的字符大小写,以及XPath中缺少/ContentItem,从而阻止了原始查询的工作。解决这个问题,你就可以开始工作了:
set @xml.modify('
replace value of
(/Content/ContentItems/ContentItem[ContentKey="abc"]/Body/text())[1]
with "Success"
')发布于 2015-12-02 04:23:58
优先: XML区分大小写。因此,您需要确保在XPath中正确调用标记。
Second:看一下这个例子...
set @xml.modify
(
'
replace value of (/Content/ContentItems/ContentItem/Body/text())[1]
with (
if (/Content/ContentItems/ContentItem/ContentKey/text() = "abc") then "Success"
else ""
)
')
select @xmlhttps://stackoverflow.com/questions/34027906
复制相似问题