In this article, we will discuss the SQL Server @@ROWCOUNT and ROWCOUNT system objects by going through practical examples that cover most of these system objects usage.

在本文中,我们将通过涵盖大多数系统对象用法的实际示例来讨论SQL Server @@ ROWCOUNT和ROWCOUNT系统对象。

用法 (Usage)

SQL Server @@ROWCOUNT is a system variable that is used to return the number of rows that are affected by the last executed statement in the batch.

SQL Server @@ ROWCOUNT是一个系统变量,用于返回受批处理中最后执行的语句影响的行数。

The rows affecting statement can be any INSERT, UPDATE, DELETE or SELECT statement that is executed directly before the @@ROWCOUNT execution, taking into consideration that both the rows affecting statement and the system variable calling query are in the same execution.

考虑到影响行的语句和调用查询的系统变量都在同一执行中,影响行的语句可以是直接在@@ ROWCOUNT执行之前执行的任何INSERT,UPDATE,DELETE或SELECT语句。

@@ROWCOUNT is used frequently in the loops to prevent the infinite loops and stop the current process when all the target rows are processed. It is also used for error handling to check the number of affected rows within the statement.

@@ ROWCOUNT在循环中经常使用,以防止无限循环并在处理所有目标行时停止当前进程。 它还用于错误处理,以检查语句中受影响的行数。

例子 (Examples)

Let us go through several examples that show the different ROWCOUNT usage scenarios.

让我们看几个显示不同ROWCOUNT使用方案的示例。

所选行数 (Number of selected rows)

The simple use for the SQL Server @@ROWCOUNT system variable is calling it directly after a SELECT statement. In the below T-SQL query, a SELECT query is used to retrieve the top 100 records from the Employee table, and in the same execution, we retrieve the number of rows that are read from the source table, which is 100 rows as in the example, as shown below:

SQL Server @@ ROWCOUNT系统变量的简单用法是在SELECT语句后直接调用它。 在下面的T-SQL查询中,使用SELECT查询从Employee表中检索前100条记录,并且在同一执行中,我们检索从源表中读取的行数,即100行。该示例,如下所示:

If we try to modify the previous query to retrieve the top 1000 records from the target table, then try to check the value stored in the SQL Server @@ROWCOUNT system variable, you will see that the returned value is 290, not 1000, as the previous SELECT statement read the whole source table’s rows, which is 290 only, as shown below:

如果我们尝试修改前一个查询以从目标表中检索前1000条记录,然后尝试检查存储在SQL Server @@ ROWCOUNT系统变量中的值,您将看到返回值是290,而不是1000,因为前面的SELECT语句读取整个源表的行,只有290行,如下所示:

If you run two SELECT statements in the same execution, followed by calling @@ROWCOUNT, you will see that it will return the number of rows returned from the second SELECT statement. In the below script. The first SELECT statement returned 290 rows and the second SELECT statement returned 296 rows. The SQL Server @@ROWCOUNT will return 296, which is the number of rows affected by the second SELECT statement, as shown below:

如果您在同一执行中运行两个SELECT语句,然后调用@@ ROWCOUNT,您将看到它将返回从第二个SELECT语句返回的行数。 在下面的脚本中。 第一个SELECT语句返回290行,第二个SELECT语句返回296行。 SQL Server @@ ROWCOUNT将返回296,这是第二条SELECT语句影响的行数,如下所示:

更新的行数 (Number of updated rows)

The SQL Server @@ROWCOUNT system variable can be used also to check the number of rows that are affected by an UPDATE statement. In the following script, the value of the @@ROWCOUNT system variable, which reflects the number of rows updated by that statement, is retrieved in the same execution of the UPDATE statement, as shown below:

SQL Server @@ ROWCOUNT系统变量还可用于检查受UPDATE语句影响的行数。 在以下脚本中,在同一执行UPDATE语句的过程中检索了@@ ROWCOUNT系统变量的值,该值反映了该语句更新的行数,如下所示:

删除的行数 (Number of deleted rows)

You can also use the SQL Server @@ROWCOUNT system variable to return the number of rows that are affected by the executed DELETE statement. In the script below, the value of the @@ROWCOUNT system variable, which reflects the number of rows deleted by the executed statement, is retrieved in the same execution of the DELETE statement, as follows:

您还可以使用SQL Server @@ ROWCOUNT系统变量来返回受已执行的DELETE语句影响的行数。 在下面的脚本中,在执行DELETE语句的相同过程中,检索@@ ROWCOUNT系统变量的值(该值反映了已执行的语句删除的行数),如下所示:

批量执行 (Batch execution)

In the batch executions, the SQL Server @@ROWCOUNT system variable will store the number of rows affected by the last batch execution. For example, if we try to execute an INSERT statement 20 times, using the GO 20 statement, then check the value stored in the @@ROWCOUNT system variable, you will find that the value equal to 1. This is because each INSERT statement in this query affects only one row. So, the last INSERT statement will insert one row then store that rows number in the system variable, as shown below:

在批处理执行中,SQL Server @@ ROWCOUNT系统变量将存储受最后一次批处理影响的行数。 例如,如果我们尝试使用GO 20语句执行INSERT语句20次,然后检查存储在@@ ROWCOUNT系统变量中的值,则您会发现该值等于1。这是因为,其中的每个INSERT语句此查询仅影响一行。 因此,最后一条INSERT语句将插入一行,然后将该行号存储在系统变量中,如下所示:

如果我在单独的执行中调用它会怎样? (What will happen if I call it in a separate execution?)

An important thing to consider here is that, if you try to run a specific query then call the SQL Server @@ROWCOUNT in a different execution, the value returned will not reflect the correct number of rows affected by the previous query, and will return 1, which is the number of rows affected by the @@ROWCOUNT calling statement, as shown below:

这里要考虑的重要事项是,如果您尝试运行特定的查询,然后在不同的执行中调用SQL Server @@ ROWCOUNT,则返回的值将无法反映受上一查询影响的正确行数,并且将返回1,即受@@ ROWCOUNT调用语句影响的行数,如下所示:

控制流量 (To control the flow)

The SQL Server @@ROWCOUNT can be used in the IF statement condition to control the flow of the statement, based on the number of rows returned from the last statement. For example, a SELECT statement is used to retrieve a number of rows from the source table, then an IF condition statement is used to check the number of rows retrieved from that table and print a relative comment, as shown below:

根据从最后一条语句返回的行数,可以在IF语句条件中使用SQL Server @@ ROWCOUNT来控制语句的流向。 例如,使用SELECT语句从源表中检索许多行,然后使用IF条件语句检查从该表中检索的行数并打印相对注释,如下所示:

控制循环 (Control a loop)

The SQL Server @@ROWCOUNT system variable can be used also to control the loop and stop it when the number of rows matches the specified condition reach a specific value, rescuing it from falling in an infinite loop. In the script below, a WHILE loop will continue executing the entire statement, that is used to delete all rows with NULL GUIDs in batches, and stop executing when the number of rows that match that condition equal to 0, as shown below:

SQL Server @@ ROWCOUNT系统变量还可以用于控制循环,并在符合指定条件的行数达到特定值时停止循环,以免陷入无限循环。 在下面的脚本中,WHILE循环将继续执行整个语句,该语句用于批量删除具有NULL GUID的所有行,并在与该条件匹配的行数等于0时停止执行,如下所示:

为什么它返回零? (Why it returns zero?)

In SQL Server, many statements will reset the value stored in @@ROWCOUNT to 0. These statements include DEALLOCATE CURSOR, CLOSE CURSOR, PRINT, RAISERROR, BEGIN TRANSACTION, COMMIT TRANSACTION, BEGIN TRY/CATCH or END TRY/CATCH.

在SQL Server中,许多语句会将@@ ROWCOUNT中存储的值重置为0。这些语句包括DEALLOCATE CURSOR,CLOSE CURSOR,PRINT,RAISERROR,BEGIN TRANSACTION,COMMIT TRANSACTION,BEGIN TRY / CATCH或END TRY / CATCH。

In the below script, we execute a SELECT statement within a TRY block but this statement will fail due to divide by zero error and another SELECT statement within the CATCH block will be executed instead. If we try to check the number of rows affected by the SELECT query to identify which SELECT query is executed, we will see that the result returned from this variable is 0 as it is reset by the END CATCH block, as shown below:

在下面的脚本中,我们在TRY块内执行SELECT语句,但是该语句将由于除零错误而失败,而将在CATCH块内执行另一个SELECT语句。 如果尝试检查受SELECT查询影响的行数以标识执行了哪个SELECT查询,我们将看到该变量返回的结果为0,因为它被END CATCH块重置,如下所示:

To overcome this issue, we need to store the number of affected rows into another variable immediately after executing each statement, in order to use that value later.

为了克服这个问题,我们需要在执行每个语句后立即将受影响的行数存储到另一个变量中,以便以后使用该值。

In the following script, we defined a variable and store the number of rows in that variable after the SELECT statements executed inside both the TRY and CATCH blocks, then return that user variable, where it will reflect the number of rows affected by the SELECT query inside the CATCH block, as shown below:

在下面的脚本中,我们定义了一个变量,并在TRY和CATCH块内执行SELECT语句之后,在该变量中存储了行数,然后返回该用户变量,它将反映受SELECT查询影响的行数在CATCH块中,如下所示:

行数> 20亿! (Number of Rows > 2 billion!)

The SQL Server @@ROWCOUNT system variable will not work if the number of rows affected by the query exceeds the 2 billion rows. This is because the data type of that system variable is INT.

如果受查询影响的行数超过20亿行,则SQL Server @@ ROWCOUNT系统变量将不起作用。 这是因为该系统变量的数据类型是INT。

To overcome this issue, SQL Server introduces the ROWCOUNT_BIG system function, which returns the number of rows affected by a specific query in the BIGINT data type.

为解决此问题,SQL Server引入了ROWCOUNT_BIG系统函数,该函数以BIGINT数据类型返回受特定查询影响的行数。

It can be called directly after the statement you are interested in checking it, as shown clearly below:

您可以在感兴趣的语句之后直接调用它,如下所示:

控制行数 (Control the Number of rows)

SET ROWCOUNT is a system object that enforces the SQL Server Engine to stop processing the query after the specified number of rows are returned, which is somehow similar to the TOP clause!

SET ROWCOUNT是一个系统对象,它强制SQL Server Engine在返回指定的行数后停止处理查询,这在某种程度上类似于TOP子句!

Microsoft does not recommend using SET ROWCOUNT with DELETE, INSERT, and UPDATE statements and uses the TOP syntax instead, as it will not affect these statements in a future release of SQL Server.

Microsoft不建议将SET ROWCOUNT与DELETE,INSERT和UPDATE语句一起使用,而应使用TOP语法,因为它不会影响SQL Server的未来版本中的这些语句。

In the below script, the SET ROWCOUNT is used to override the TOP keyword in the SELECT statement and limit the number of rows to a smaller value, where the SELECT query will return only 10 records, as shown below:

在下面的脚本中,SET ROWCOUNT用于覆盖SELECT语句中的TOP关键字,并将行数限制为较小的值,其中SELECT查询将仅返回10条记录,如下所示:

You can turn the ROWCOUNT off, and allow the statement to return all rows, by setting its value to 0. For example, the SELECT statement below will return the 200 rows, without being limited by the specific number of rows, as shown below:

您可以关闭ROWCOUNT,并通过将其值设置为0来允许语句返回所有行。例如,下面的SELECT语句将返回200行,而不受特定行数的限制,如下所示:

SET ROWCOUNT can be also used to limit the number of deleted rows, which is not recommended. In the script below, we can see that the number of rows that match the specified condition is 30 rows and we use the SET ROWCOUNT to delete only 5 rows, as shown below:

SET ROWCOUNT也可用于限制已删除的行数,不建议这样做。 在下面的脚本中,我们可以看到符合指定条件的行数是30行,并且我们使用SET ROWCOUNT仅删除5行,如下所示:

As the SET ROWCOUNT will be deprecated for the INSERT, UPDATE and DELETE statements, we can easily replace it with the TOP syntax and delete only 5 rows, as shown below:

由于将不推荐使用INSERT,UPDATE和DELETE语句使用SET ROWCOUNT,因此我们可以轻松地用TOP语法替换它,并仅删除5行,如下所示:

结论 (Conclusion)

We explored the SQL Server @@ROWCOUNT and ROWCOUNT system objects by going through several examples in this article. This system variable returns the number of rows affected by the last executed statement in the batch and is extremely beneficial in loops and error handling.

通过阅读本文中的几个示例,我们探索了SQL Server @@ ROWCOUNT和ROWCOUNT系统对象。 此系统变量返回受批处理中最后执行的语句影响的行数,这在循环和错误处理中特别有用。

翻译自: https://www.sqlshack.com/working-with-sql-server-rowcount/

使用SQL Server ROWCOUNT相关推荐

  1. SQL Server Rowcount 和 top 以及@@Rowcount 区别

    rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10 select * from 表A 这样的查询只会返回表A中的前10条数据 ...

  2. SQL Server中Rowcount与@@Rowcount的用法 和set nocount on 也会更新@@Rowcount

    rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10 select * from 表A 这样的查询只 ...

  3. SQL Server中@@ROWCOUNT的用法

    转自:http://www.studyofnet.com/news/146.html 本文导读:@@ROWCOUNT返回上一语句受影响的行数!和@ERROR一样的特性,在每一条语句执行后都将被重置,如 ...

  4. sql server 中 SET ROWCOUNT 的具体用法

    SET ROWCOUNT 使 Microsoft® SQL Server™ 在返回指定的行数之后停止处理查询. 语法 SET ROWCOUNT{ number | @number_var } 参数 n ...

  5. mysql sql rowcount,SQL SERVER的ROWCOUNT关键字

    因为仰望ORACLE,所以一直都以为SQL SERVER很笨. 据传SQL 2005有了RowID的东西,可以解决TOP排序的问题.可惜还没有机会体验.在SQL 2000中写存储过程,总会遇到需要TO ...

  6. oracle存储过程rowcount用法,sql server的rowcount关键字_数据库技巧

    因为仰望ORACLE,所以一直都以为SQL SERVER很笨. 据传SQL 2005有了RowID的东西,可以解决TOP排序的问题.可惜还没有机会体验.在SQL 2000中写存储过程,总会遇到需要TO ...

  7. SQL Server 2005下的分页SQL

    其实基本上有三种方法: 1.使用SQL Server 2005中新增的ROW_NUMBER 几种写法分别如下: 1SELECT TOP 20 * FROM (SELECT 2   ROW_NUMBER ...

  8. python 使用pymssql连接sql server数据库

    Python连接SQL Server数据库 - pymssql使用基础 ----原文地址:http://www.cnblogs.com/baiyangcao/p/pymssql_basic.html ...

  9. sql server 海量数据速度提升:SQL优化-索引(11) 【转】

    12.高效的TOP 事实上,在查询和提取超大容量的数据集时,影响数据库响应时间的最大因素不是数据查找,而是物理的I/0操作.如: select top 10 * from ( select top 1 ...

最新文章

  1. java开发 中台
  2. Windows 下用 SecureCRT 连接 VirtualBox 中的 Ubuntu
  3. CSS3实现侧边栏快速定位的隐藏和消失
  4. 第三次学JAVA再学不好就吃翔(part54)--StringBuffer类的添加功能
  5. 用java做一个模拟彩票程序_JAVA模拟----- 彩票机子-----抽奖过程的实例化
  6. Ubuntu根目录下各文件夹的功能详细介绍
  7. Java--Socket通信
  8. Qt 设置窗体或控件渐变消失
  9. java编译命令带参数_java编译命令基础知识点
  10. 给初创业者的几点建议
  11. “好师父”如何破解大学生就业难题
  12. tf.nn.embedding_lookup()函数
  13. 在场景中添加光线——在反光表面添加镜面高光
  14. 基于微信小程序的毕业设计题目(27)php校园跑腿小程序(含开题报告、任务书、中期报告、答辩PPT、论文模板)
  15. Selenium分布式运行:SeleniumGrid
  16. 人工智能之高等数学2
  17. TVS二极管和稳压二极管区别和原理
  18. 【古代文学论文】沈德潜诗学思想的调和格调与文化意蕴(节选)
  19. 微型计算机拆卸步骤,《微型计算机拆卸》PPT课件.ppt
  20. python爬虫爬取京东商品评价_python爬取京东商品信息及评论

热门文章

  1. oracle 修改字段长度_Oracle字段长度引起的思考length()和lengthb()
  2. win7 mysql8.0.11安装教程_mysql8.0.13下载与安装图文教程(示例代码)
  3. ValueError: Variable conv1/weights already exists.
  4. git submodule子模块管理
  5. [Vue warn]: Unknown custom element: <Top> - did you register the component correctly?
  6. JavaScript学习(七十二)—严格模式
  7. JavaScript学习(七十)—函数中this的指向问题
  8. 详解用backgroundImage解决图片轮播切换
  9. linux服务器上svn的log_SVN如何查看修改的文件记录
  10. 马斯克:不要把员工变成“螺丝钉”