How to Limit the Number of Records Returned in MS SQL
It can be counterproductive to get a lot of records from a query if you don't need to see all of them. The ROWCOUNT option is available in Microsoft SQL Server to limit the number of records returned by a query. Running a query without this option can take a long time if your table holds many records. Use the ROWCOUNT option to optimize your queries and improve performance in your database applications.
Instructions
-
-
1
Start Microsoft SQL Server Management Studio and expand the database you want to use. Click the New Query icon to create a new query.
-
2
Type the following to build a Select query using the ROWCOUNT option to limit the number of records returned to four:
SET ROWCOUNT 4;
SELECT dbo.Products.ProductName
FROM dbo.Products
GO
-
-
3
Replace dbo.Products with the name of your database table. Replace ProductName with the name of the field you want to query. Replace the number 4 with the number of records you would like to have returned.
-
4
Press "F5" to run your query.
-
1
References
- Photo Credit Thinkstock/Comstock/Getty Images