@@rowcount 的范围? [英] Scope of @@rowcount?

查看:30
本文介绍了@@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 的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆