Solved

Simple Paging help?

Posted on 2012-03-20
1
263 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach 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.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now