SET ROWCOUNT

Transact-SQL Reference

Transact-SQL Reference

SET ROWCOUNT

Causes Microsoft® SQL Server™ to stop processing the query after the specified number of rows are returned.

Syntax

SET ROWCOUNT { number | @number_var }

Arguments

number | @number_var

Is the number (an integer) of rows to be processed before stopping the given query.

Remarks

It is recommended that DELETE, INSERT, and UPDATE statements currently using SET ROWCOUNT be rewritten to use the TOP syntax. For more information, see DELETE, INSERT, or UPDATE.

The setting of the SET ROWCOUNT option is ignored for INSERT, UPDATE, and DELETE statements against remote tables and local and remote partitioned views.

To turn this option off (so that all rows are returned), specify SET ROWCOUNT 0.

Note  Setting the SET ROWCOUNT option causes most Transact-SQL statements to stop processing when they have been affected by the specified number of rows. This includes triggers and data modification statements such as INSERT, UPDATE, and DELETE. The ROWCOUNT option has no effect on dynamic cursors, but it limits the rowset of keyset and insensitive cursors. This option should be used with caution and primarily with the SELECT statement.

SET ROWCOUNT overrides the SELECT statement TOP keyword if the rowcount is the smaller value.

The setting of SET ROWCOUNT is set at execute or run time and not at parse time.

Permissions

SET ROWCOUNT permissions default to all users.

Examples

SET ROWCOUNT stops processing after the specified number of rows. In this example, note that x rows meet the criteria of advances less than or equal to $5,000. However, from the number of rows returned by the update, you can see that not all rows were processed. ROWCOUNT affects all Transact-SQL statements.

USE pubs
GO
SELECT count(*) AS Cnt
FROM titles 
WHERE advance >= 5000
GO

Here is the result set:

Cnt       
----------- 
11          

(1 row(s) affected)

Now, set ROWCOUNT to 4 and update all rows with an advance of $5,000 or more.

-- SET ROWCOUNT to 4.
SET ROWCOUNT 4
GO
UPDATE titles
SET advance = 5000
WHERE advance >= 5000
GO

See Also

SET