Solved

Simple Paging help?

Posted on 2012-03-20
1
265 Views
Last Modified: 2012-03-20
I am trying to setup simple paging - which works great - but if I limit the intial select to say, SELECT TOP 50, *, etc. - The below will not work as the rowNumber is out - do I need to somehow re-seed the row number or...?

DECLARE @startIndex int, 
@pageSize int , 
@totalCount int

SET @startIndex = NULL
SET @pageSize = NULL

DROP TABLE #summary

SELECT *, ROW_NUMBER() OVER(ORDER BY hcpcs DESC) AS 'RowNumber' INTO #summary FROM dbMasterdata.dbo.tblADDB
ORDER BY hcpcs

UPDATE #summary

SET @totalCount = (SELECT COUNT(*) FROM #summary)

PRINT @totalCount

SET @startIndex = @pageSize * (@startIndex - 1)

SET @pageSize = CASE WHEN @pageSize IS NULL THEN @totalCount ELSE @pageSize END
SET @StartIndex = CASE WHEN @StartIndex IS NULL THEN 0 ELSE @StartIndex END

SELECT * FROM #summary
WHERE RowNumber BETWEEN (@StartIndex + 1) AND (@StartIndex + @PageSize)
ORDER BY RowNumber 

Open in new window

0
Comment
Question by:tbaseflug
1 Comment
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 37744121
dont use temp file, keep it simple just use following code

SET @pageSize = CASE WHEN @pageSize IS NULL THEN @totalCount ELSE @pageSize END
SET @StartIndex = CASE WHEN @StartIndex IS NULL THEN 0 ELSE @StartIndex END

select * from (
SELECT *, ROW_NUMBER() OVER(ORDER BY hcpcs DESC) AS 'RowNumber'
FROM dbMasterdata.dbo.tblADDB
) x where rn between (@StartIndex + 1) AND (@StartIndex + @PageSize)
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question