TSQL:如何使用另一个相关表中的 xml 标记值更新 xml 标记的值?

TSQL: How can I update the value of an xml tag with the value of an xml tag from another related table?(TSQL:如何使用另一个相关表中的 xml 标记值更新 xml 标记的值?)
本文介绍了TSQL:如何使用另一个相关表中的 xml 标记值更新 xml 标记的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何使用另一个相关表中的 xml 标记值更新 xml 标记的值?

How can I update the value of an xml tag with the value of an xml tag from another related table?

像这样:

UPDATE v2
 SET
 [xml].modify ('replace value of (//TAG1/text())[1] 
                with "CAST(v1.[xml].query(''//TAG2'') AS NVARCHAR(MAX))"')
FROM 
 table2 v2, 
 table1 v1 
WHERE
 v2.id = v1.id

推荐答案

我不认为您可以一步完成 - 但如果您使用的是 SQL Server 2008,则可以分两步完成:

I don't think you can do this in a single step - but you can do it in two steps, if you're on SQL Server 2008:

DECLARE @NewValue NVARCHAR(50)

SELECT @NewValue = [xml].value('(//TAG2)[1]', 'NVARCHAR(50)')
FROM dbo.v1 
WHERE id = 1

UPDATE dbo.v2
SET [xml].modify('replace value of (//TAG1/text())[1] with sql:variable("@NewValue")')
WHERE id = 1

在您的 replace value of 中指定 sql:variable 的能力 XQuery 是 SQL Server 2008 中的一个新功能 - 所以如果你被困在 2005,这不幸的是,这行不通.

The ability to specify a sql:variable in your replace value of XQuery is a new feature in SQL Server 2008 - so if you're stuck on 2005, this won't work, unfortunately.

这篇关于TSQL:如何使用另一个相关表中的 xml 标记值更新 xml 标记的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SQL query to group by day(按天分组的 SQL 查询)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
sql group by versus distinct(sql group by 与不同)
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)
Count number of records returned by group by(统计分组返回的记录数)
SQL GROUP BY CASE statement with aggregate function(带聚合函数的 SQL GROUP BY CASE 语句)