What’s wrong with the following statement? I’m using SQL Server 2008.
use Demo;
SELECT * FROM users
limit 0 , 30
I got:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '0'.
That’s really weird. I tried Google but didn’t find much info.
LIMITis a MySQL keyword. Use theTOPorROWCOUNTkeywords in MS SQL Server.Note that
TOPcan accept a variable, e.g.SELECT TOP( @NumberOfRows ) * FROM Foo;See: How to use LIMIT keyword in SQL Server 2005? (also valid for 2008)
Depending on how
LIMITis used, there is an important difference betweenLIMITandTOP(ranges/pages of data versus just capping the number of results). In that case, the MS SQL syntax is more verbose; usually the ROW_NUMBER() function does the trick combined with some simple logic to calculate the values which are valid for the desired page.Simple Range Selection Example