[英]TSQL xml.modify replace value for node with value
I need to update a XML node in a SQL Server column. 我需要在SQL Server列中更新XML节点。
I see many examples of using an xpath and identifying the node using the attribute. 我看到了许多使用xpath并使用属性标识节点的示例。
The XML I am working with is auto generated and does not come with attributes on the nodes I need. 我正在使用的XML是自动生成的,并且在我需要的节点上没有属性。 How would I construct the update statement to use another value in the node to find it?
我将如何构造更新语句以使用节点中的另一个值来找到它?
I'm hoping to be able to write something like this: 我希望能够写这样的东西:
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
You're in the right direction. 您的方向正确。 Only wrong character-casing, as mentioned in the other answer, and missing
/ContentItem
in the XPath that prevent the original query to work. 如另一个答案中所述,只有错误的字符大小写,以及XPath中缺少
/ContentItem
的内容会阻止原始查询正常工作。 Fix that and you're good to go : 解决这个问题,您就可以开始了:
set @xml.modify('
replace value of
(/Content/ContentItems/ContentItem[ContentKey="abc"]/Body/text())[1]
with "Success"
')
First: XML is case sensitive. 首先: XML区分大小写。 So you'll want to make sure you're calling out the tags correctly in your XPath.
因此,您需要确保在XPath中正确调用了标签。
Second: Have a look at this example... 第二:看看这个例子...
set @xml.modify
(
'
replace value of (/Content/ContentItems/ContentItem/Body/text())[1]
with (
if (/Content/ContentItems/ContentItem/ContentKey/text() = "abc") then "Success"
else ""
)
')
select @xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.