2017-03-05 90 views
2

不工作在我下面的查询:限制SQL

Select * from DimCustomer 
order by MiddleName desc LIMIT 5 

获得以下错误:

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'LIMIT'.

+2

像人类语言,SQL有方言。确保始终检查您正在使用的特定数据库引擎的文档。 –

回答

1

SQL Server不使用limit这样的,它使用top代替。

select top 5 * from DimCustomer order by MiddleName desc 

如果您正在寻找分页,您可以在SQL Server中使用offsetfetch 2012+

select * 
from DimCustomer 
order by MiddleName desc 
offset 0 rows 
fetch next 5 rows only; 

更多的模式和分页选项,点击这里:Pagination with offset/fetch : A better way - Aaron Betrand

+0

但为什么LIMIT在这种情况下不起作用? – user3652369

+2

@ user3652369因为你在SQL服务器上? – SqlZim

+0

是的,我在使用MSSQL 2012的SQL服务器上,数据库是AdventureWorksDW2012 – user3652369