Returns the number of affected rows in the previous statement! Similar to @ ERROR, each statement is reset after execution. If it is used in the future, you need to save the variable to a local variable. Any statement not returned sets this variable to 0! For example, IF statements are frequently used. Let alone code! DECLARE @ RowCountVarINTUpdateAF_CarOilSETOilT
Returns the number of affected rows in the previous statement! Similar to @ ERROR, each statement is reset after execution. If it is used in the future, you need to save the variable to a local variable. Any statement not returned sets this variable to 0! For example, IF statements are frequently used. Let alone code! DECLARE @ RowCountVar INT Update AF_CarOil SET OilT
Returns the number of affected rows in the previous statement! Similar to @ ERROR, each statement is reset after execution. If it is used in the future, you need to save the variable to a local variable. Any statement not returned sets this variable to 0! For example, IF statements are frequently used. Let alone code!
DECLARE @ RowCountVar INT
Update AF_CarOil SET OilType = 'gasoline 5' -- after execution @ ROWCOUNT is 2
SET @ RowCountVar = @ ROWCOUNT -- after execution @ ROWCOUNT is 1
IF @ ROWCOUNT = 1 -- after execution @ ROWCOUNT is 0
BEGIN
PRINT 'Affected rows are 1'
PRINT @ ROWCOUNT
END
IF @ RowCountVar <> 0
BEGIN
PRINT 'Affected rows: '+ STR (@ RowCountVar)
END
-----------------------------------------------------------------
(2 rows affected)
The number of affected rows is 1.
0
Affected rows: 2
Analysis: In the above Code, after executing the Update statement, the number of affected rows is two, and then the affected rows are saved to the locally declared variable, in fact, the number of rows affected by the value assignment statement is one line, and the next IF statement jumps. This is the key. This is not because the number of rows affected by the Update statement is 1, instead, the process of assigning @ ROWCOUNT to a local variable is changed to 1. After execution, the value of @ ROWCOUNT = 1 is assigned to 0 again!