Problem regarding stored procedure paging.
Posted on 2007-03-23
We are using SQL 2000 and have many pages where we required stored proc paging. We're currently using this template to provide our paging:
CREATE PROCEDURE [dbo].[usp_PageResults_NAI]
DECLARE @first_id int, @startRow int
-- A check can be added to make sure @startRowIndex isn't > count(1)
-- from employees before doing any actual work unless it is guaranteed
-- the caller won't do that
-- Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid
-- Now, set the row count to MaximumRows and get
-- all records >= @first_id
SET ROWCOUNT @maximumRows
SELECT e.*, d.name as DepartmentName
FROM employees e
INNER JOIN Departments D ON
e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID
SET ROWCOUNT 0
This method is very fast and easy to use but the problem with it is that it stores the unique column ID as a start and end point on where to get page 2, page 3 etc. It only works when you're bring back results that are ASC or DESC on a incremental unique column.
We want to find a very fast solution to paging like this but with different sorting. We want to bring back members who have updated their profile recently, or were online, which means their unique id's won't be incremental anymore.
A) Is it wise to use the above paging method for columns other than unique ID's and if so, how would we go about it.
B) Is there a better paging solution out there?