@ @ROWCOUNT Global Variables

Source: Internet
Author: User
Tags rowcount

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!

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.