10

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

我想将所有Age30 到 40 的属性更改如下:

 <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" />
4

3 回答 3

11

从您问题的早期版本看来,您的 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 
于 2012-09-23T15:35:59.493 回答
8

尝试这个:

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
于 2012-09-23T15:18:18.310 回答
2

modify 方法是您的响应。但是如果你需要一个条件,你可以在这个方法的 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;
于 2012-09-23T09:36:04.757 回答