[英]SQL XML column - update child node value based on other nodes
I am not used to use xml in an sql column and have a question about updating the content of that column. 我不习惯在sql列中使用xml,并且对更新该列的内容有疑问。
I have a table ( TableXML
) with a column ( ColumnXML
) containing the following xml hierarchy : Xml/Content/Queues/list/Item/
我有一个表(
TableXML
),其中的列( ColumnXML
)包含以下xml层次结构: Xml/Content/Queues/list/Item/
Each Item have a /Name
, a list of PluginsProperties/Item
每个项目都有一个
/Name
,一个PluginsProperties/Item
列表
and each of these other item have a /key
and value
每个其他项目都有一个
/key
和value
For example : 例如 :
<Xml>
<Content Tr="1">
<Queues Tr="12">
<list Tr="13">
<Item Tr="14">
<Name Tr="2">Data Load Exception</Name>
<PluginProperties Tr="15">
<Item Tr="16">
<key Tr="2">MSMQQueueType</key>
<value Tr="2">PrivateQueue</value>
</Item>
...........more items
</PluginProperties>
</Item>
...........more items
</list>
</Queues>
</Content>
</Xml>
What I would like to do : 我想做的是:
Update the value of the /Xml/Content/Queues/list/Item/PluginProperties/Item/value
tag to PublicQueue
将
/Xml/Content/Queues/list/Item/PluginProperties/Item/value
标记的/Xml/Content/Queues/list/Item/PluginProperties/Item/value
为PublicQueue
where the /Xml/Content/Queues/list/Item/PluginProperties/Item/key
is MSMQQueueType
/Xml/Content/Queues/list/Item/PluginProperties/Item/key
是MSMQQueueType
and the /Xml/Content/Queues/list/Item/Name
is Data Load Exception
并且
/Xml/Content/Queues/list/Item/Name
为Data Load Exception
No "Queue item" other than the one with name Data Load Exception
should be affected and no "PluginProperties item" other than the one with key MSMQQueueType
should be affected for it. 除了名称为
Data Load Exception
项目之外,其他“队列项目”均不会受到影响,对于具有MSMQQueueType
键的项目而言,除其他之外的“ PluginProperties项目”也不会受到影响。
Thanks! 谢谢! =)
=)
you can use replace value of
clause to do this 您可以使用子句的
replace value of
来执行此操作
if it was a single node then it can be done in with single statement like below 如果它是单个节点,则可以使用以下单个语句完成
update TableXML
set columnXML.modify('
replace value of ((/Xml/Content/Queues/list/Item/PluginProperties/Item/value)[1]/text())[1] with ''PublicQueue''')
where columnXML.value('((/Xml/Content/Queues/list/Item/PluginProperties/Item/key)[1]/text())[1]','varchar(50)') = 'MSMQQueueType'
and columnXML.value('((/Xml/Content/Queues/list/Item/Name)[1]/text())[1]','varchar(50)') = 'Data Load Exception'
AS there exists many nodes, we need to get the count of the nodes and do it using while loop like below 由于存在许多节点,我们需要获取节点数并使用如下所示的while循环进行计数
declare @elements int
select @elements = ISNULL(columnXML.value('count(/Xml/Content/Queues/list/Item/PluginProperties/Item)', 'int'),0)
from TableXML
while @elements > 0
begin
update TableXML
set columnXML.modify
('replace value of ((/Xml/Content/Queues/list/Item/PluginProperties/Item/value)[sql:variable("@elements")]/text())[1]
with ''PublicQueue''')
where columnXML.value('((/Xml/Content/Queues/list/Item/PluginProperties/Item/key)[sql:variable("@elements")]/text())[1]','varchar(50)') = 'MSMQQueueType'
and columnXML.value('((/Xml/Content/Queues/list/Item/Name)[sql:variable("@elements")]/text())[1]','varchar(50)') = 'Data Load Exception'
set @elements = @elements - 1
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.