限制SQL查询

时间:2013-04-11 15:25:54

标签: sql sql-server sql-limit

如何在以下查询中将结果限制为10? 我使用SQLSRV。

SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

3 个答案:

答案 0 :(得分:6)

使用TOP

SELECT TOP 10 Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

答案 1 :(得分:1)

使用

select top(10) Id, Firstname, Surname ....

答案 2 :(得分:0)

answer by kevingessner肯定是最简单的方式。

我只是觉得我会为了好玩而抛出一些替代品。

SET ROWCOUNT 10

 SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

SET ROWCOUNT 0

或者更复杂的方式:

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