使用SQL Server 2008表中的新值更新Xml属性
我在SQLServer2008中有一个表,它有一些列。其中一列是Xml格式的 我想更新一些属性 例如,我的Xml列的名称是使用SQL Server 2008表中的新值更新Xml属性,sql,xml,sql-server-2008,xquery,Sql,Xml,Sql Server 2008,Xquery,我在SQLServer2008中有一个表,它有一些列。其中一列是Xml格式的 我想更新一些属性 例如,我的Xml列的名称是XmlText,它在前5行中的值如下: <Identification Name="John" Family="Brown" Age="30" /> <Identification Name="Smith" Family="Johnson" Age="35" /> <Identification Name="Jessy"
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" />
修改方法是您的响应。但若您需要一个条件,您可以在该方法的with部分中使用if表达式
DECLARE @t TABLE (RecordXML XML);
Declare @xml XML
SET @xml = '<Root>
<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" />
</Root>'
INSERT @t VALUES (@xml);
Declare @value nvarchar(50)
DECLARE @oldvalue nvarchar(50)
SET @value = '40'
SET @oldvalue = '30'
Declare @update_count xml
select @update_count = @xml.query('count(/Root/Identification/@Age[.=sql:variable("@oldvalue")])')
Declare @number int
select @number = convert(int, (convert(nvarchar(50), @update_count)))
declare @Node int
set @Node = 1
while @Node <= @number
begin
UPDATE
@t
SET
RecordXML.modify('replace value of
(/Root/Identification/@Age[.=sql:variable("@oldvalue")])[1] with sql:variable("@value")')
WHERE
RecordXML.exist('/Root/Identification[@Age=sql:variable("@oldvalue")]') = 1;
set @Node = @Node + 1
end
SELECT * FROM @t;
DECLARE@t表(RecordXML);
声明@xml
SET@xml=
'
插入@t值(@xml);
声明@value nvarchar(50)
声明@oldvalue nvarchar(50)
设置@value='40'
设置@oldvalue='30'
声明@update\u count xml
选择@update\u count=@xml.query('count(/Root/Identification/@Age[.=sql:variable(@oldvalue”)))
声明@number int
选择@number=convert(int,(convert(nvarchar(50),@update_count)))
声明@Node int
设置@Node=1
当@Node时,尝试以下方法:
declare @xml XML
SET @xml = '<Root>
<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" />
</Root>'
DECLARE @nodeCount int
DECLARE @i int
SET @i = 1
SELECT @nodeCount = @xml.value('count(/Root/Identification/@Age)','int')
PRINT 'Number of nodes found: ' + STR(@nodeCount)
WHILE (@i <= @nodeCount)
BEGIN
Set @xml.modify('replace value of (/Root/Identification/@Age)[.=30][1] with "40"')
SET @i = @i + 1
END
SELECT @xml
declare@xml
SET@xml=
'
声明@nodeCount int
声明@i int
设置@i=1
选择@nodeCount=@xml.value('count(/Root/Identification/@Age)','int')
打印“找到的节点数:”+STR(@nodeCount)
而(@i从您的问题的早期版本来看,您的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
declare@T表(XMLText-xml)
插入@T值(“”)
插入@T值(“”)
插入@T值(“”)
插入@T值(“”)
插入@T值(“”)
更新@T集
modify((/Identification/@Age)[1]的值替换为“40”)
其中XMLText.value('(/Identification/@Age)[1]',int')=30
挑选*
来自@T
Hi Kapil Khandelwal。非常感谢您的编辑。SQL=结构化查询语言-它不是产品。我们需要知道哪个数据库系统(以及哪个版本)您正在使用-SQL Server、Oracle、IBM DB2、PostgreSQL等。您好,谢谢您的回答。但您的回答并不完全正确,因为它只更改第一行,但我需要一个将所有年龄属性更改为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