Get the number of affected rows @@rowCount after the execution of the previous statement in Sqlserver

from:http://blog.163.com/rihui_7/blog/static/212285143201381343240404/

 

 

Returns the number of rows affected by the previous statement. If the number of rows is greater than 2 billion, use  ROWCOUNT_BIG .

Topic link icon Transact-SQL Syntax Conventions

@@ROWCOUNT

Transact-SQL statements can set the value of @@ROWCOUNT in the following ways:

  • Set @@ROWCOUNT to the number of rows affected or read. Rows may or may not be sent to the client.

  • The @@ROWCOUNT from the previous statement execution is preserved.

  • Reset @@ROWCOUNT to 0 without returning the value to the client.

Statements that perform simple assignments always set the @@ROWCOUNT value to 1. Do not send any lines to the client.  Examples of these statements are: SET @ local_variable , RETURN, READTEXT, and Select statements without a query, such as SELECT GETDATE() or SELECT  ' Generic Text ' .

Statements that make assignments in a query or use RETURN set the @@ROWCOUNT value to the number of rows affected by the query or the number of rows read by the query, for example: SELECT @ local_variable  = c1 FROM t1.

数据操作语言 (DML) 语句将 @@ROWCOUNT 值设置为受查询影响的行数,并将该值返回到客户端。 DML 语句不会将任何行发送到客户端。

DECLARE CURSOR 和 FETCH 将 @@ROWCOUNT 值设置为 1。

EXECUTE 语句保留前一个 @@ROWCOUNT。

USE、SET <option>、DEALLOCATE CURSOR、CLOSE CURSOR、BEGIN TRANSACTION 或 COMMIT TRANSACTION 等语句将 ROWCOUNT 值重置为 0。

以下示例执行 UPDATE 语句并使用 @@ROWCOUNT 来检测是否更改了任何一些行。

USE AdventureWorks2012; GO UPDATE HumanResources.Employee  SET JobTitle = N'Executive' WHERE NationalIDNumber = 123456789 IF @@ROWCOUNT = 0 PRINT 'Warning: No rows were updated'; GO

        
        
返回上一语句受影响的行数!和@ERROR一样的特性,在每一条语句执行后都将被重置,如果将来使用需要将变量保存到局部变量中。任何不返回的语句都将这个变量置为0!比如经常使用的IF语句。废话少说,上代码!
DECLARE @RowCountVar INT
Update AF_CarOil SET OilType = '五号汽油'-- 执行后@@ROWCOUNT为2
SET @RowCountVar = @@ROWCOUNT -- @@ROWCOUNT is 1 after execution
IF @@ROWCOUNT = 1  -- @@ROWCOUNT is 0 after execution
BEGIN
    PRINT 'The number of rows affected is 1'
PRINT @@ROWCOUNT
END
IF @RowCountVar <> 0
BEGIN
    PRINT 'The number of rows affected is:' + STR(@RowCountVar)
END
-----------------------------------------------------------------
 
(2 rows affected)
The number of rows affected is 1
0
Affected rows are: 2
Analysis: In the above code, after the Update statement is executed, the number of affected lines is 2 lines, and then the number of affected lines is saved to the pre-declared local variable. The number of lines affected by the assignment statement is actually 1 line. The jump is made in the following IF statement. This place is the key. This is not because the number of rows affected by the Update statement is 1, but it becomes 1 in the process of assigning @@ROWCOUNT to the local variable. After executing the judgment of IF @@ROWCOUNT = 1, the value of @@ROWCOUNT is reassigned to 0!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970601&siteId=291194637