Saturday, 31 July 2010

@@ROWCOUNT

Transact-SQL statements can set the value in @@ROWCOUNT in the following ways:

1) Set @@ROWCOUNT to the number of rows affected or read. Rows may or may not be sent to the client.
2) Preserve @@ROWCOUNT from the previous statement execution.
3) Reset @@ROWCOUNT to 0 but do not return the value to the client.

a)Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET @local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'.

b)Statements that make an assignment in a query or use RETURN in a query set the @@ROWCOUNT value to the number of rows affected or read by the query, for example: SELECT @local_variable = c1 FROM t1.

c)Data manipulation language (DML) statements set the @@ROWCOUNT value to the number of rows affected by the query and return that value to the client. The DML statements may not send any rows to the client.

d)DECLARE CURSOR and FETCH set the @@ROWCOUNT value to 1.

e)EXECUTE statements preserve the previous @@ROWCOUNT.

f)Statements such as USE, SET