@@rowcount 的范围?

编程入门 行业动态 更新时间:2024-10-28 08:27:46
本文介绍了@@rowcount 的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

@@rowcount 的范围是什么?MSDN 没有提到它的范围.

返回受最后一条语句影响的行数.

好的.

在我的 SP - 我插入一个表,它有一个 insert 触发器,当一个新值插入到表中时,它会另一个插入.

问题:

@@rowcount 将引用哪个范围?(触发器或 SP)?

解决方案

当然,这篇文章是针对 SQL Server 2000 的,但人们希望版本之间的范围不会改变.根据文章 触发器如何影响 SQL Server 2000 中的 ROWCOUNT 和 IDENTITY, @@ROWCOUNT 不会受到触发器的影响.

特别是:

即使基表上有触发器,在 SQL Server 2000 中使用 @@ROWCOUNT 也是安全的.触发器不会扭曲您的结果;你会得到你所期望的.即使设置了 NOCOUNT,@@ROWCOUNT 也能正常工作.

因此,如果您更新了三行,而触发器在其他地方更新了五行,您将获得 @@ROWCOUNT 为 3.

此外,来自 GBN 在 SQL Server 中的回答 - 正在使用@@ROWCOUNT 在多线程应用程序中安全吗?:

@@ROWCOUNT 对作用域和连接都是安全的.

事实上,它只读取该连接和作用域的最后一条语句行数.

What is the scope of @@rowcount ?MSDN doesn't mention its scope.

Returns the number of rows affected by the last statement.

Ok.

In my SP — I'm inserting into a table , which has a insert trigger which does another insertion when a new value is inserted to the table.

Question:

To which scope the @@rowcount will refer ? (trigger or SP) ?

解决方案

Granted, the article is for SQL Server 2000, but one would hope the scope doesn't change between versions. According to the article How triggers affect ROWCOUNT and IDENTITY in SQL Server 2000, @@ROWCOUNT will not be affected by triggers.

Specifically:

It’s safe to use @@ROWCOUNT in SQL Server 2000 even when there is a trigger on the base table. The trigger will not skew your results; you’ll get what you expect. @@ROWCOUNT works correctly even when NOCOUNT is set.

So if you update three rows, and the trigger updates five rows elsewhere, you'll get a @@ROWCOUNT of 3.

Also, from GBN's answer in SQL Server - is using @@ROWCOUNT safe in multithreaded applications?:

@@ROWCOUNT is both scope and connection safe.

In fact, it reads only the last statement row count for that connection and scope.

更多推荐

@@rowcount 的范围?

本文发布于:2023-10-18 23:38:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rowcount

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!