[英]Changing xml node value in SQL with xquery [SQL Server]
I have example XML in one of the table column: 我在表格列之一中有示例XML:
<Root>
<A>
<C>c</C>
<D>d</D>
</A>
<B>b</B>
</Root>
How can I replace value of A node into something like that: 如何将A节点的值替换为类似的内容:
<Root>
<A>
<E>e</E>
<F>f</F>
</A>
<B>b</B>
</Root>
I tried the following solution 我尝试了以下解决方案
DECLARE @var varchar(100);
SET @var = '<E>e</E><F>f</F>'
SET @xml.modify('replace value of (/Root/A/text())[1] with sql:variable("@var")');
but it did not work... 但这没用...
regards 问候
I do not think it is possible to use replace to replace nodes, only values. 我认为不可能使用replace来替换节点,只能替换值。 You can use delete and then insert instead.
您可以使用delete ,然后插入 。
declare @xml as xml = '
<Root>
<A>
<C>c</C>
<D>d</D>
</A>
<B>b</B>
</Root>'
declare @var xml
set @var = '<E>e</E><F>f</F>'
set @xml.modify('delete /Root/A/*')
set @xml.modify('insert sql:variable("@var") into (/Root/A)[1]')
select @xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.