在SQL Server更新XML节点值(Update XML node value in SQL S

2019-07-18 09:54发布

我需要在群ID字段更新为0。我已经找到了如何检索值,我现在遇到了问题更新它。

任何帮助将GE太棒了!

<ProblemProfile>
  <GroupID>-1</GroupID>
  <LayoutID>1</LayoutID>
  <MyQueries>false</MyQueries>
</ProblemProfile>

Declare @Result xml
set @Result = convert(xml,(select ProfileXML from profiles where id = 23))

SELECT x.value('.', 'int') ID
FROM @Result.nodes('/ProblemProfile/GroupID') as R(x)

更新

我现在要做的就是更新每一行的群ID有“富”的价值

declare @foo int
set @foo = -1

UPDATE  profiles
SET  ProfileXML.modify('replace value of (/ProblemProfile/GroupID/text())[1] with "0"')
WHERE  ProfileXML.value('(/ProblemProfile/GroupID)[1]', 'int') = @foo

这是唯一的更新满足此条件的第一行。 我如何将更新的每一行?

更新2该声明的作品。 原来为第一个节点的数据库结构可以是不同的。 一个简单的//GroupID...etc更新的每一行。 它始终是绊倒我们哈哈愚蠢的小事情。

Answer 1:

你可以这样做:

UPDATE 
   dbo.profiles
SET 
   ProfileXML.modify('replace value of (/ProblemProfile/GroupID/text())[1] with "0"')
WHERE
   id = 23

看看这篇文章的SQL Server 2005 XQuery和XML-DML的更多详细信息,你可以用做什么.modify关键字(插入,删除,替换等)。

PS:为了得到价值,这将是一个更容易完成这一点:

SELECT ProfileXML.value('(/ProblemProfile/GroupID)[1]', 'int') as ID
FROM dbo.profiles
WHERE id = 23

(当然,除非你需要的XML作为别的SQL变量以后)



Answer 2:

改变内部元素的文本的最简单方法

UPDATE [TableName]
   SET  
      [fieldName].modify('replace value of (root/elementName/text())[1] with "wBob"')
GO


文章来源: Update XML node value in SQL Server