Transact SQL Other Articles Software Reviews
Using @@Rowcount to Determine the Number of Rows Affected by a SQL Statement
The @@Rowcount function will be set after any statement that changes or returns rows. In the following statement, the RowsReturned column will display the number of rows selected by the previous select statement:
SELECT * FROM AUTHORS
While this example could obviously be rewritten to use a SELECT COUNT... type of syntax, the @@Rowcount function is useful in that it is also set after any statement that changes rows. As such it is useful in determining how many rows were affected by an INSERT or an UPDATE statement. For example, the following SQL statement changes the city column in the authors table of the pubs database from Salt Lake City to Oakland:
UPDATE authors SET city = 'Oakland' WHERE city = 'Salt
The statement will return the number of rows changed as the RowsChanged column.