SET ROWCOUNT (T-SQL)

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.

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 each 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

  

Here is the result set:

The command(s) completed successfully.

  

See Also

SET

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.