@@ROWCOUNT in SQL Server

The system function @@ROWCOUNT is used to return the number of rows that were impacted by the last statement that was executed.

The number of affected rows will be counted and put in the @@ROWCOUNT variable if the DML operation is successful. There are numerous uses for this.

  • The @@ROWCOUNT can be used as the endpoint for the loop expression
  • If necessary, the application should return the number of rows impacted by the user transaction.
  • The entire number of rows in a table can be stored in this temporary variable.
  • During the IF ELSE condition.


Example-1: In case of providing the information about the transaction

BEGIN TRAN
INSERT INTO Dept (DeptNo, DName) VALUES (60, ‘IT’)
IF @@ROWCOUNT > 0
PRINT ‘Department Information has been stored’
ELSE PRINT ‘Not Inserted’
COMMIT

Result:

(1 row affected)
Department Information has been stored

Example-2: In case of breaking in the transaction process.

IF @@ROWCOUNT < 1
BEGIN
SELECT @vcErrorMsg = ”No tickets have been assigned # ” + ””+ CONVERT(VARCHAR(10), @UserID)
RETURN
END

Hope you find this article helpful.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s