Sql paging through large amounts of data

DECLARE @PageSize int
DECLARE @PageNumber int
DECLARE @iBeginRecord int
DECLARE @iEndRecord int
SET @PageNumber = 1
SET @PageSize = 20
SET @iBeginRecord= ((@PageNumber 1) * @PageSize) ;
SET @iEndRecord = @iBeginRecord + @PageSize;
SELECT PriceRank, ProductName, UnitPrice
FROM(  SELECT ProductName, UnitPrice, ROW_NUMBER() OVER(ORDER BY UnitPrice DESC) AS PriceRank FROM Products )  AS ProductsWithRowNumber 
WHERE PriceRank > @iBeginRecord AND PriceRank <= @iEndRecord

Leave a comment