How to use LIMIT keyword in SQL Server 2005? How to use LIMIT keyword in SQL Server 2005? sql-server sql-server

How to use LIMIT keyword in SQL Server 2005?


If you take a look at the SELECT statement in SQL Server Books Online, then you'll see that you can limit the resultset by using the TOP keyword.

SELECT TOP 1 * FROM employee


SELECT TOP 1 * FROM Employee ORDER BY newid()

You have to use newid() for it to be evaluated once per row.


I'm using this fairly simple one (SQL2005) to limit the number of rows returned, which will work with a value provided by a stored procedure parameter as well.

DECLARE @Limit intSET @Limit = 10SELECT TOP (@Limit) Col1, Col2 FROM SomeTable