Home > SQL Server > Limit the current session to process fixed number of rows – SET ROWCOUNT – SQL Server

Limit the current session to process fixed number of rows – SET ROWCOUNT – SQL Server


SET ROWCOUNT: This set statement is used to limit the SQL Server current session to process a fixed number of rows. This statement requires a numeric value to set up the row limit. The value 0 (zero) is used to set unlimited number of rows.

RowCount statement will not take effect for insert, update and delete statements in the future version of SQL Server. We must use TOP keyword in the SQL statement to do so. TOP keyword will not take effect when we use RowCount set statement with less value than the top statement.

Usage

SET ROWCOUNT  constant numeric value or @number value variable

Example

1. Set rowcount to 5. SQL Server only process 5 rows.

pic12. Set ROWCOUNT to 5 and TOP(3). SQL Server process 3 rows only.

pic2

Because, the maximum row to process by current session is 5 and TOP has 3 row limit. So, SQL Server was able to process the request. But,this example may not have any logical challenge. So, in the next example I am going to set RowCount value to 5 and the TOP(7) .

pic1Now, SQL Server processed only 5 rows. So, We should pay careful attention while using ROWCOUNT set statements to avoid limited resultset.

Advertisements
  1. No comments yet.
  1. May 24, 2014 at 12:22 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: