user3652369
user3652369

Reputation: 153

LIMIT not working in SQL

In my below Query:

Select * from DimCustomer 
order by MiddleName desc LIMIT 5

Getting below error:

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

Upvotes: 15

Views: 54411

Answers (1)

SqlZim
SqlZim

Reputation: 38063

Sql Server doesn't use limit like that, it uses top instead.

 select top 5 * from DimCustomer order by MiddleName desc

If you are looking for pagination, you can use offset and fetch in sql server 2012+

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

For more patterns and options for pagination, check here: Pagination with offset / fetch : A better way - Aaron Betrand

Upvotes: 28

Related Questions