Thursday, June 24, 2010

Pagination on the Sql

To get the data form the data base based on UI pagination we can use the following Query.This query make use of the With Statement for achiving this task

DECLARE @StartRow INT,@PageSize INT,@PageIndex INT
SELECT @PageIndex = 5, @PageSize = 5
-- Retriving Paged Data
DECLARE @FirstRow INT,@LastRow INT
set @StartRow=@PageIndex*@PageSize

SELECT @FirstRow = @StartRow + 1, @LastRow = @PageSize + @StartRow;
WITH Members AS
(

      SELECT Coloumn1, Coloumn2, Coloumn3, Coloumn4, Coloumn5
      ROW_NUMBER() OVER (ORDER BY Coloumn1 ASC) AS RowNumber

      FROM TableName1
)
SELECT RowNumber,Coloumn1, Coloumn2, Coloumn3, Coloumn4, Coloumn5 FROM
Members
WHERE RowNumber BETWEEN @FirstRow AND @LastRow OR @PageSize = -1
ORDER BY RowNumber ASC;

0 comments:

Post a Comment