sql - 使用 SQL Server 2008 表中的新值更新 Xml 属性

标签 sql xml sql-server-2008 xquery

我在 SQL Server 2008 中有一个表,它有一些列。这些列之一是 Xml 格式 我想更新一些属性。

例如我的 Xml 列的名称是 XmlText 并且它在前 5 行的值是这样的:

 <Identification Name="John"  Family="Brown"     Age="30" /> 
 <Identification Name="Smith" Family="Johnson"   Age="35" /> 
 <Identification Name="Jessy" Family="Albert"    Age="60" />
 <Identification Name="Mike"  Family="Brown"     Age="23" />
 <Identification Name="Sarah" Family="Johnson"   Age="30" />

我想更改所有 30 到 40 之间的 Age 属性,如下所示:

 <Identification Name="John"  Family="Brown"     Age="40" /> 
 <Identification Name="Smith" Family="Johnson"   Age="35" /> 
 <Identification Name="Jessy" Family="Albert"    Age="60" />
 <Identification Name="Mike"  Family="Brown"     Age="23" />
 <Identification Name="Sarah" Family="Johnson"   Age="40" />

最佳答案

从您问题的早期版本看来,您的 XML 实际上位于表中的不同行上。如果是这种情况,您可以使用它。

update YourTable set
  XMLText.modify('replace value of (/Identification/@Age)[1] with "40"')
where XMLText.value('(/Identification/@Age)[1]', 'int') = 30

使用表变量的工作示例。

declare @T table(XMLText xml)

insert into @T values('<Identification Name="John"  Family="Brown"   Age="30" />')
insert into @T values('<Identification Name="Smith" Family="Johnson" Age="35" />') 
insert into @T values('<Identification Name="Jessy" Family="Albert"  Age="60" />')
insert into @T values('<Identification Name="Mike"  Family="Brown"   Age="23" />')
insert into @T values('<Identification Name="Sarah" Family="Johnson" Age="30" />')

update @T set
  XMLText.modify('replace value of (/Identification/@Age)[1] with "40"')
where XMLText.value('(/Identification/@Age)[1]', 'int') = 30

select *
from @T 

关于sql - 使用 SQL Server 2008 表中的新值更新 Xml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545964/

相关文章:

c# - 如何从 SQL 表中动态获取单元格值?

java - 如何在 Java 中通过 DOM 创建以下 XML 标记

javascript - Javascript 中带有前导零的数字

sql - 将 NVARCHAR 转换为货币值

php - 显示来自mysql数据库的阿拉伯语数据

mysql - 如何从 'Case When' 条件开始计数

sql - 为什么 IN 运算符在传递重复值(value1、value1 ....)时返回不同的选择

c# - 在 foreach 循环中解析 XML

sql - 在一个数据库中用SQL Server 2008中的另一个数据库中的值更新记录吗?

sql-server-2008 - 过度设计的数据库设计