LIMIT
is a MySQL keyword. Use the TOP
or ROWCOUNT
keywords in MS SQL Server.
Note that TOP
can 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 LIMIT
is used, there is an important difference between LIMIT
and TOP
(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
SELECT * FROM
(
SELECT
ROW_NUMBER() OVER( ORDER BY SomeColumn ASC ) AS RowNumber,
AnotherColumn
FROM dbo.MyTable
) Q
WHERE RowNumber BETWEEN 20 AND 30; -- these integers can be variables