@ ROWCOUNT global variable

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.