我有两个表:tags
和devices
,每个表都有XML列:tag_config
,device_config
。我试图根据device_config
中节点的值更新tag_config
XML中一个节点的值。
我使用以下语句根据整数列更新device_config
中的节点:
update d
set device_config.modify('replace value of (//plc_int_device_status/text())[1] with sql:column("t.uid")')
from devices d join tags t on
d.device_name = t.name
是否可以使用sql:column("t.uid")
列的查询()替换tag_config
以获取特定节点的值?
答案 0 :(得分:0)
您可以先使用可更新的CTE 提取所需的值,然后更新其他列:
DECLARE @mockup TABLE(ID INT IDENTITY, XML1 XML, XML2 XML);
INSERT INTO @mockup VALUES
('<root><SomeElement test="x">InnerText</SomeElement></root>'
,'<root><OtherElement test="y">OtherText</OtherElement></root>'
)
,('<root><SomeElement test="a">abc</SomeElement></root>'
,'<root><OtherElement test="b">xyz</OtherElement></root>'
);
SELECT XML1 FROM @mockup;
WITH updateableCTE AS
(
SELECT XML1
,XML2.value('(/root/OtherElement/text())[1]','nvarchar(max)') AS NewText
FROM @mockup
)
UPDATE updateableCTE
SET XML1.modify('replace value of (/root/SomeElement/text())[1] with sql:column("NewText")');
SELECT XML1 FROM @mockup;