Is there some lib or function i can use to take my basic sql statement and transform the limit statement to a sql server compatible statement?
2 Answers
The closest equivalent of MySQL's LIMIT function is the TOP function. So
Select..
From Table
LIMIT 10
In SQL Server this would be:
Select TOP 10 ...
From Table
Order By ...
ADDITION
Per your comments, you are asking about mimicking the offset parameter on LIMIT. You can do that with a CTE in SQL Server 2005+:
With NumberedItems As
(
Select ...
, ROW_NUMBER() OVER ( Order By ... ) As Num
From Table
)
Select ...
From NumberedItems
Where Num Between 5 And 20
4 Comments
Thomas
@acidzombie24 - If you show us the query that you would run in MySQL that you want to run in SQL Server, we can show you how to write the equivalent.
Thomas
@acidzombie24 - Ah, you want to know about the offset parameter which is a different kettle of fish. I've updated my post to illustrate how you can achieve that using a CTE (and assuming you are using SQL Server 2005+). There is no 1:1 equivalent function to the LIMIT with an offset parameter in SQL Server.
Thomas
@KM - Granted, although if you are going to use a CTE with ROW_NUMBER, you might as well go all the way.
Sounds like you're wanting to use LIMIT's offset functionality for pagination, in which case the SO question
"What is the best way to paginate results in MS SQLServer" has a very good accepted answer.
LIMITinstead ofTOP?