LIMIT and OFFSET in MS SQL Server
How to Hack MySQL Paging Functionality into Microsoft SQL Server
Web developers using PHP and MySQL have a crucial piece of functionality that classic ASP developers working with Microsoft SQL Server don’t. It’s pagination. Using MySQL’s LIMIT and OFFSET commands you can very easily add pagination to recordsets that you want to display on web pages. If you’ve spent any amount of time searching for a way to do this with ASP and SQL Server, you know that the code is pretty hard to find. You’re lucky if you find it at all. The few tutorials on the ‘net tend to be overly complicated and pretty bad hacks, usually involving convoluted and resource-intensive subqueries on top of subqueries. This solution is certainly not the best, and it, too, is a resource hog, but it’s the only one I’ve got, so I’m sharing.
The sample stored procedure will query a Microsoft SQL Server “music” table for records. It returns two recordsets. The first recordset contains the meta data you’ll need to add your buttons or drop-down combo box navigation in ASP. The second recordset contains the data. (Tip: Use set rs = rs.nextrecordset to move to the second recordset in ASP.)
As a bonus I’m also including the code to sort the results, something I’ve never been able to find anywhere else.



October 3rd, 2006 at 6:37 am
Totally! I mean, without my CMP3 codes and the offset Makadoo font scribbler, I doubt Wordpress would function under the demands for pagination that my site requires. If only the Alt.Mov inverter was cross-spawned into MySQL’s T-frame originator, we’d all be Ctrl-Alt-tastic!……..Pubes.
March 13th, 2007 at 2:40 am
Really, your script is very useful. I used it in my procedure, its working as needed. Thanks alot for your work.
But I wonder if my database has some ten thousand records, then there will be load on Server because it has to fetch ten thousand records every time into temp table, just to retrieve only 10 records. Really “LIMIT”less SQL Server
June 13th, 2008 at 11:36 pm
So true Manda, its a type of solution but theres a performance hit while you database grows. Almost as bad as using the viewstate!
July 16th, 2008 at 9:21 am
Hi,
In your stored procedure:
set rowcount @perPage
isn’t usefull , you limit the rows with @lbound and @ubound