tags:

views:

420

answers:

4
+6  A: 
select top(@count) * from users

If @count is a constant, you can drop the parentheses:

select top 42 * from users

(the latter works on SQL Server 2000 too, while the former requires at least 2005)

Mehrdad Afshari
+2  A: 
SELECT TOP 10 *
FROM Users

Note that if you don't specify an ORDER BY clause then any 10 rows could be returned, because "first 10 rows" doesn't really mean anything until you tell the database what ordering to use.

LukeH
+1 for the `order by` distinction. Order without an `order by` is *never* guaranteed by SQL Server.
Eric
+1  A: 

You can also use rowcount, but TOP is probably better and cleaner, hence the upvote for Mehrdad

SET ROWCOUNT 10
SELECT * FROM dbo.Orders
WHERE EmployeeID = 5
ORDER BY OrderDate

SET ROWCOUNT 0
NickAtuShip
this is a way of parameterizing top N rows in versions of SQL Server pre-2005
Russ Cam
A: 

SELECT * FROM USERS LIMIT 10;

RIDDHI