Sunday, November 21, 2021

How to use @@ROWCOUNT in SQL Server

 How to use @@ROWCOUNT in SQL Server

The T-SQL language provides us with the @@ROWCOUNT system variable. This variable returns the number of rows affected by the last executed statement in the batch. In this tip, we’ll demonstrate how to use this variable and point out some caveats.

 

BEGIN TRAN

UPDATE [Sales].[SalesOrderHeader]

SET [SubTotal] = [SubTotal] * 1.1; -- 10% increase

IF @@ROWCOUNT = 0

    PRINT 'Something went wrong!'

ELSE PRINT 'Rows were updated...'

--COMMIT

ROLLBACK

 

No comments:

Post a Comment