2013-04-11 203 views
2

如何在下列查詢中將結果限制爲10? 我使用SQLSRV。限制SQL查詢

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

SELECT後加上TOP 10。 – 2013-04-11 15:29:10

回答

6

使用TOP

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

+0

謝謝kevingessner – 2013-04-11 15:42:43

1

使用

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

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