我有一个存储的 SQL 过程,它已经声明了具有这种结构的 xml 变量
I have a stored SQL procedure, which has declared xml variable with this kind of structure
<a>
<b>4</b>
<b>18</b>
<b>2</b>
</a>
我需要为这个 XML 数据做一些类似 UPDATE a SET b=b-1 WHERE b>@MyIntVariable 的事情.在 MS Transact SQL 中执行此操作的最佳方法是什么?
I need to do something like UPDATE a SET b=b-1 WHERE b>@MyIntVariable for this XML data. What is the best way to do it in MS Transact SQL?
modify 函数最适合处理 xml 数据.请参阅 http://msdn.microsoft.com/en-us/library/ms190675(v=sql.105).aspx.
The modify function would be the most appropriate for manipulating xml data. See http://msdn.microsoft.com/en-us/library/ms190675(v=sql.105).aspx.
DECLARE @NUM INT = 10
DECLARE @xml XML = N'
<a>
<b>4</b>
<b>18</b>
<b>2</b>
</a>
';
SELECT @XML;
DECLARE @COUNT INT
SET @COUNT = @XML.value ('count(/a/b)', 'int');
WHILE @COUNT > 0
BEGIN
SET @XML.modify('replace value of (/a/b[sql:variable("@COUNT")]/text())[1] with
(
if ((/a/b[sql:variable("@COUNT")])[1] > sql:variable("@NUM")) then
(/a/b[sql:variable("@COUNT")])[1] - 1
else
(/a/b[sql:variable("@COUNT")])[1] cast as xs:double ?
)
')
SET @COUNT = @COUNT - 1;
END
SELECT @XML
这篇关于用xml到sql按条件更新所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
我可以在不编写 SQL 查询的情况下找出数据库列表Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?(我可以在不编写 SQL 查询的情况下
如何创建对 SQL Server 实例的登录?How to create a login to a SQL Server instance?(如何创建对 SQL Server 实例的登录?)
如何通过注册表搜索知道SQL Server的版本和版本How to know the version and edition of SQL Server through registry search(如何通过注册表搜索知道SQL Server的版本和版本)
为什么会出现“数据类型转换错误"?使用 ExWhy do I get a quot;data type conversion errorquot; with ExecuteNonQuery()?(为什么会出现“数据类型转换错误?使用 ExecuteNonQuery()?)
如何将 DataGridView 中的图像显示到 PictureBox?How to show an image from a DataGridView to a PictureBox?(如何将 DataGridView 中的图像显示到 PictureBox?)
WinForms 应用程序设计——将文档从 SQL Server 移动WinForms application design - moving documents from SQL Server to file storage(WinForms 应用程序设计——将文档从 SQL Server 移动到文件存