Dynamically replacing the value of a node in XML DML(动态替换 XML DML 中节点的值)
本文介绍了动态替换 XML DML 中节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在为此苦苦挣扎:如何替换文本等于某个变量值的无类型 XML 列中的节点值?可能吗?
I am struggling with this now: How do you replace the value of a node in an untyped XML column where the text is equal to a certain variable value? Is it possible?
我的 XML:
<attrs>
<attr>ManualInsert</attr>
<attr>ManualInsert2</attr>
<attr>ManualInsert4</attr>
<attr>ManualInsert8</attr>
</attrs>
我的尝试:
DECLARE @OldValue Varchar(255) = 'ManualInsert'
DECLARE @NewValue Varchar(255) = 'ReplacedValue'
UPDATE
Labels
SET
Attributes.modify('replace value of (/attrs/attr/text())[1]
with
if ((/attrs/attr/text() = sql:variable("@OldValue")))
then sql:variable("@NewValue")
else () ')
WHERE
Id = 2000046
消息:(0 行受影响)
DECLARE @OldValue Varchar(255) = 'ManualInsert'
DECLARE @NewValue Varchar(255) = 'ReplacedValue'
UPDATE
Labels
SET
Attributes.modify('replace value of (/attrs/attr[text() = sql:variable("@OldValue")])[1]
with sql:variable("@NewValue")')
WHERE
Id = 2000046
留言:
Msg 2356, Level 16, State 1, Line 7
XQuery [Labels.Attributes.modify()]: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content, found 'element(attr,xdt:untyped) ?'
预期结果:
<attrs>
<attr>ReplacedValue</attr>
<attr>ManualInsert2</attr>
<attr>ManualInsert4</attr>
<attr>ManualInsert8</attr>
</attrs>
推荐答案
modify('replace value of (/attrs/attr[. = sql:variable("@OldValue")]/text())[1]
with sql:variable("@NewValue")')
您的第二次尝试实际上只是缺少指定应该替换的是 text() .这也可以.
Your second attempt is actually just missing to specify that it is the text() that should be replaced. This will also work.
modify('replace value of (/attrs/attr[text() = sql:variable("@OldValue")]/text())[1]
with sql:variable("@NewValue")')
这篇关于动态替换 XML DML 中节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:动态替换 XML DML 中节点的值


猜你喜欢
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01