Problem regarding stored procedure paging.

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]
(
    @startRowIndex int,
    @maximumRows int
)
AS

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

GO

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?
TsoukiasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dqmqCommented:
OK, I"ll give it a shot. Pardon any typo's because I don't have a system to test right now.

First, adapt your given example to dyanamically generate rownumber independent of the employeeID. That looks like this:

CREATE  PROCEDURE [dbo].[usp_PageResults_NAI]
(
    @startRowIndex int,
    @maximumRows int
)
AS      
SELECT e.*, d.name as DepartmentName
--derive ordinal rownum from employeeID sequence, which is a unique key
, (select count (*)+1 from FROM employees e2            
      inner join departments d2 on e2.departmentID = d2.departmentID
      where e2.employeeID < e.employeeID
  )  as rownum
FROM employees e
   INNER JOIN Departments D ON
       e.DepartmentID = d.DepartmentID
WHERE rownum between  @startRowIndex and (@startRowIndex + @maximumRows  - 1)
ORDER BY e.EmployeeID

Then expand to support a different sort. For this example, I will sort by Dept Name


CREATE  PROCEDURE [dbo].[usp_PageResults_NAI_byDept ]
(
    @startRowIndex int,
    @maximumRows int
)
AS      
SELECT e.*, d.name as DepartmentName
--derive ordinal rownum, this time from name.  Since name is not unique, use the unique key
--employeeID as a tie breaker
, (select count (*)+1 from FROM employees e2
      inner join departments d2 on e2.departmentID = d2.departmentID
      where d2.name < d.name
            or (d2.name = d.name and e2.employeeID < e2.employeeID)  -- tie breaker
  )  as rownum
FROM employees e
   INNER JOIN Departments D ON
       e.DepartmentID = d.DepartmentID
WHERE rownum between  @startRowIndex and (@startRowIndex + @maximumRows  - 1)
ORDER BY d.Name, e.employeeID      

Note the essence of the solution comes from how rownum is generated by the inner select. Once you 'get' the idea, you can make the inner select order asc or desc by any column or columns you like, provided you use the unique key to break any ties.

good luck
--dQmQ  


0
TsoukiasAuthor Commented:
That solution looks definitely along the lines of what I was hoping for - but I forgot to mention that ideally I'd like a dynamic solution, ie, passing in the column I wanted to order/sort on?  Is this going to be possible?
0
dqmqCommented:
1. To support dynamic paging, I incorporated a view. The basic view is shown below.  You may need to tweak it a bit to include all of the columns you want.  The purpose of the view is so that when you get to the dynamic SQL, all columns can be referenced by the same table alias.

CREATE VIEW [dbo].[EmployeeDepartmentsView]
AS
SELECT e.*, d.Name FROM employees e
   INNER JOIN Departments d ON
       e.DepartmentID = d.DepartmentID
GO


2. Then modify the (non-dynamic) SQL to use the view.  Get this working to your satisfaction before
moving to step 3.

CREATE  PROCEDURE [dbo].[usp_PageResults_NAI ]
(
    @startRowIndex int,
    @maximumRows int,
)
AS
SELECT *
, (select count (*)+1
   from EmployeeDepartmentsView v2
   where v2.name < v1.name
            or (v2.name = v1.name and v2.employeeID < v1.employeeID)  
  )  as rownum
FROM EmployeeDepartmentsView V1
WHERE rownum between  @startRowIndex and (@startRowIndex + @maximumRows  - 1)
ORDER BY v1.Name, v1.employeeID

3.  OK, now introduce the additional parameter and dynamic SQL

CREATE  PROCEDURE [dbo].[usp_PageResults_NAI ]
(
    @startRowIndex int,
    @maximumRows int,
    @sortColumn varchar(128) = 'EmployeeID'
)
AS
DECLARE @sql VARCHAR(8000)
SET @sql = '
 SELECT *, (select count (*)+1
 FROM EmployeeDepartmentsView v2
 WHERE '
    +  ' v2.'  + @sortcolumn
    + ' < v1.' + @sortcolumn
    + ' or (v2.' + @sortcolumn
    + ' = v1.' + @sortcolumn + '
  AND v2.employeeID < v1.employeeID)) AS rownum
  FROM EmployeeDepartmentsView V1
  WHERE rownum between  @startRowIndex and (@startRowIndex + @maximumRows  - 1)
  ORDER BY v1.Name, v1.employeeID'

SELECT @sql  --for testing only
EXEC (@sql)  


     
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.