Returns the number of rows affected by the previous statement! The same features as @error are reset after each statement is executed, and if future use requires saving the variable to a local variable. Any statements that do not return this variable to 0! For example, if statements that are used frequently. Nonsense less say, on the code!
DECLARE @RowCountVar INT
Update af_caroil SET oiltype = ' No. Fifth gasoline '--After execution @ @ROWCOUNT to 2
SET @RowCountVar = @ @ROWCOUNT--After execution @ @ROWCOUNT is 1
IF @ @ROWCOUNT = 1--After execution @ @ROWCOUNT to 0
BEGIN
The number of rows affected by PRINT ' is 1 '
PRINT @ @ROWCOUNT
End
IF @RowCountVar <> 0
BEGIN
The number of rows affected by PRINT ' is: ' + STR (@RowCountVar)
End
-----------------------------------------------------------------
(2 rows affected)
The number of rows affected is 1
0
The number of rows affected is: 2
Analysis: After executing the UPDATE statement in the above code, the number of rows affected is 2, and then the number of rows affected is saved to a previously declared local variable, and the assignment statement actually affects 1 rows, and jumps in the IF statement below, which is the key. This is not the number of rows affected by the UPDATE statement is 1, but instead the process of assigning the @ @ROWCOUNT to a local variable becomes 1. After the execution of the IF @ @ROWCOUNT = 1 's judgment, the value of the @ @ROWCOUNT is again assigned to 0!