微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我在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

原文地址:https://www.jb51.cc/mssql/77075.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐