Set Rowcount vs Select Top with MSSQL

SET ROWCOUNT

  1. With SET ROWCOUNT you can limit the number of rows returned.
  2. if you set: SET ROWCOUNT 10;, SQL Server will return the first 10 rows of the request.
  3. Most often used to testing/development purpose.

SELECT TOP

  1. With SELECT TOP you can limit the number of rows returned.
  2. if you set: SELECT TOP 10;, SQL Server will return the first 10 rows of the request.
  3. Most often used to testing/development purpose.
  • Does this look similar?
  • Yes, it's almost the same!
  • The difference?: SELECT TOP is ANSI SQL compliant.

Data used

USE sql911;
go

SELECT sales_date,sales_qty,sales_price,emp_short
FROM sales;
go
sales_date              sales_qty                               sales_price                             emp_short
----------------------- --------------------------------------- --------------------------------------- ----------
2005-10-06 10:01:01.000 10.00                                   11.75                                   000310
2006-10-06 10:01:01.000 10.00                                   11.75                                   000310
2005-11-06 10:01:01.000 9.00                                    11.75                                   000110
2006-10-06 10:01:01.000 10.00                                   11.75                                   000310
2005-12-06 11:01:01.000 8.00                                    11.75                                   000010
2006-02-06 13:01:01.000 NULL                                    NULL                                    000190
2006-03-06 14:01:01.000 5.00                                    11.75                                   000110
2006-10-06 10:01:01.000 10.00                                   11.75                                   000190
2006-10-06 10:01:01.000 10.00                                   11.75                                   000190
2006-04-06 15:01:01.000 4.00                                    11.75                                   000030
2006-05-06 16:01:01.000 3.00                                    11.75                                   000010
2004-10-06 10:01:01.000 NULL                                    NULL                                    000310
2005-11-06 10:01:01.000 9.00                                    11.75                                   000010
2006-06-06 17:01:01.000 2.00                                    11.75                                   000190
2006-01-06 12:01:01.000 7.00                                    11.75                                   000310
2006-02-06 13:01:01.000 6.00                                    11.75                                   000110
2005-03-06 14:01:01.000 5.00                                    11.75                                   000190
2006-10-06 10:01:01.000 10.00                                   11.75                                   000110
2006-10-06 10:01:01.000 3.00                                    11.75                                   000110
2007-06-06 17:01:01.000 2.00                                    11.75                                   000010
2007-04-06 00:00:00.000 4.00                                    11.75                                   000010
2007-05-01 00:00:00.000 5.00                                    11.75                                   000030
2007-07-06 00:00:00.000 1.00                                    11.75                                   000190
2007-05-03 09:00:00.000 3.00                                    11.75                                   000010
2007-05-05 04:00:00.000 1.00                                    11.75                                   000010
2006-10-06 10:01:01.000 10.00                                   11.75                                   000030
2004-03-06 14:01:01.000 NULL                                    NULL                                    000110
2005-04-06 15:01:01.000 4.00                                    11.75                                   000010
2005-05-06 16:01:01.000 3.00                                    11.75                                   000190
2006-06-06 17:01:01.000 2.00                                    11.75                                   000030
2006-07-06 19:01:01.000 1.00                                    11.75                                   000310
2006-03-06 14:01:01.000 5.00                                    11.75                                   000310
2006-04-06 15:01:01.000 4.00                                    11.75                                   000010
2007-05-07 11:00:00.000 2.00                                    11.75                                   000010
2006-06-06 17:01:01.000 NULL                                    NULL                                    000110
2006-07-06 19:01:01.000 1.00                                    11.75                                   000190
2005-10-06 10:01:01.000 10.00                                   11.75                                   000030
2007-05-08 15:00:00.000 1.50                                    11.75                                   000010
2005-12-06 11:01:01.000 8.00                                    11.75                                   000190
2006-01-06 12:01:01.000 7.00                                    11.75                                   000030
2007-05-07 11:30:00.000 2.00                                    10.50                                   000030
2007-05-08 13:30:00.000 1.00                                    10.00                                   000030
2006-07-06 19:01:01.000 1.00                                    11.75                                   000010
2007-05-09 14:00:00.000 1.25                                    10.00                                   000110
2007-11-06 10:01:01.000 9.00                                    11.75                                   000030
2007-05-10 14:00:00.000 5.00                                    10.00                                   000190
2007-05-11 14:15:00.000 4.00                                    10.00                                   000190
2007-05-12 15:00:00.000 2.00                                    10.00                                   000190
2007-05-13 16:00:00.000 1.00                                    10.00                                   000190
2007-05-08 05:00:00.000 1.00                                    10.00                                   000310
2007-05-07 00:00:00.000 2.00                                    10.00                                   000310
2007-05-14 11:00:00.000 3.00                                    10.00                                   000310
2006-07-06 00:00:00.000 1.00                                    11.75                                   000190
2007-05-11 13:00:00.000 3.50                                    10.00                                   000310
2007-05-08 05:00:00.000 1.00                                    10.00                                   000010
2007-05-08 07:00:00.000 2.00                                    10.00                                   000010
2007-05-08 11:00:00.000 3.00                                    10.00                                   000010

(57 row(s) affected)

SET ROWCOUNT

SET rowcount 10;
SELECT sales_date,sales_qty,sales_price,emp_short
FROM sales;
go
sales_date              sales_qty                               sales_price                             emp_short
----------------------- --------------------------------------- --------------------------------------- ----------
2005-10-06 10:01:01.000 10.00                                   11.75                                   000310
2006-10-06 10:01:01.000 10.00                                   11.75                                   000310
2005-11-06 10:01:01.000 9.00                                    11.75                                   000110
2006-10-06 10:01:01.000 10.00                                   11.75                                   000310
2005-12-06 11:01:01.000 8.00                                    11.75                                   000010
2006-02-06 13:01:01.000 NULL                                    NULL                                    000190
2006-03-06 14:01:01.000 5.00                                    11.75                                   000110
2006-10-06 10:01:01.000 10.00                                   11.75                                   000190
2006-10-06 10:01:01.000 10.00                                   11.75                                   000190
2006-04-06 15:01:01.000 4.00                                    11.75                                   000030

(10 row(s) affected)

SELECT TOP

SELECT TOP 10 sales_date,sales_qty,sales_price,emp_short
FROM sales;
go
sales_date              sales_qty                               sales_price                             emp_short
----------------------- --------------------------------------- --------------------------------------- ----------
2005-10-06 10:01:01.000 10.00                                   11.75                                   000310
2006-10-06 10:01:01.000 10.00                                   11.75                                   000310
2005-11-06 10:01:01.000 9.00                                    11.75                                   000110
2006-10-06 10:01:01.000 10.00                                   11.75                                   000310
2005-12-06 11:01:01.000 8.00                                    11.75                                   000010
2006-02-06 13:01:01.000 NULL                                    NULL                                    000190
2006-03-06 14:01:01.000 5.00                                    11.75                                   000110
2006-10-06 10:01:01.000 10.00                                   11.75                                   000190
2006-10-06 10:01:01.000 10.00                                   11.75                                   000190
2006-04-06 15:01:01.000 4.00                                    11.75                                   000030

(10 row(s) affected)
  • Same result, identical
  • It's preferable to use SELECT TOP xx, it's ANSI SQL compliant.