I have this query with mysql :
select * from table1 LIMIT 10,20
How can I do this with Microsoft sql ?
Join Stack Overflow to learn, share knowledge, and build your career.
|
I have this query with mysql :
How can I do this with Microsoft sql ? |
|||||||||
|
|
Starting SQL SERVER 2005, you can do this...
or something like this for 2000 and below versions...
|
|||||||||||||
|
|
Clunky, but it'll work.
MSSQL's omission of a LIMIT clause is criminal, IMO. You shouldn't have to do this kind of kludgy workaround. |
|||||||||||||||||||||
|
|
This is almost a duplicate of a question I asked in October: Emulate MySQL LIMIT clause in Microsoft SQL Server 2000 If you're using Microsoft SQL Server 2000, there is no good solution. Most people have to resort to capturing the result of the query in a temporary table with a If you're using Microsoft SQL Server 2005 or later, you have a
You can also write this as a common table expression as shown in @Leon Tayson's answer. |
|||||||||
|
|
Starting with SQL SERVER 2012, you can use the OFFSET FETCH Clause:
http://msdn.microsoft.com/en-us/library/ms188385(v=sql.110).aspx This may not work correctly when the order by is not unique. If the the query is modified to ORDER BY OrderDate, the result set returned is not as expected. |
|||||
|
|
|||||
|
|
Syntactically MySQL LIMIT query is something like this:
This can be translated into Microsoft SQL Server like
Now your query
|
||||
|
|
|
This is how I limit the results in MS SQL Server 2012
NOTE: OFFSET can only be used with or in tandem to ORDER BY. To explain the code line OFFSET xx ROWS FETCH NEXT yy ROW ONLY The "xx" is the record / row number you want to start pulling from in the table.
The "yy" is the number of records / rows you want to pull from the table.
Check out the link for more info on OFFSET |
||||
|
|
|
This is one of the reasons I try to avoid using MS Server... but anyway. Sometimes you just don't have an option (yei! and I have to use an outdated version!!). My suggestion is to create a virtual table: From:
To:
Then just query:
If fields are added, or removed, "row" is updated automatically. The main problem with this option is that ORDER BY is fixed. So if you want a different order, you would have to create another view. UPDATE There is another problem with this approach: if you try to filter your data, it won't work as expected. For example, if you do:
WHERE becomes limited to those data which are in the rows between 10 and 20 (instead of searching the whole dataset and limiting the output). |
||||
|
|
|
This is a multi step approach that will work in SQL2000.
|
||||
|
|
|
|||||
|
|
If i remember correctly (it's been a while since i dabbed with SQL Server) you may be able to use something like this: (2005 and up)
|
|||||||||||||
|
Is the same as
Here's an article about implementing Limit in MsSQL Its a nice read, specially the comments. |
|||||||||||||
|