MSSQL Alternative to MySQL's LIMIT Pagination

>> Wednesday, April 4, 2012

In MySQL is quite easy to paginate, with SQL you just do

SELECT * FROM MyTable LIMIT 10, 5

That will select the first 10 rows starting from 5, oddly enough MSSQL does not have an easy way to do this, there are a lot of different ways to do it, but the easiests is using two TOPs

SELECT TOP 10 * FROM MyTable WHERE id NOT IN (SELECT TOP 5 id FROM MyTable ORDER BY Id DESC) ORDER BY Id DESC

This will work as long as you don't want to start from 0, of course, in that case a regular TOP statement will do fine.

Post a Comment

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP