Reputation: 135
How can I LIMIT the results to 10 in the following query? I use SQLSRV.
SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?
Upvotes: 2
Views: 1376
Reputation: 44032
The answer by kevingessner is certainly the easiest way.
I just thought I would throw some alternatives in for fun.
SET ROWCOUNT 10
SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?
SET ROWCOUNT 0
Or a more convoluted way:
With q
as
(
Select ROW_NUMBER() Over(Order by Id) as rn,
Id,
Firstname,
Surname
FROM Person WHERE Firstname LIKE ?
)
Select *
From q
where q.rn <= 10
Upvotes: 0
Reputation: 18995
Use TOP
:
SELECT TOP 10 Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?
Upvotes: 6