Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pagination in sql server 2005

Author  Topic 

amirs
Constraint Violating Yak Guru

260 Posts

Posted - 2009-04-07 : 09:06:12
hi
im my table their are 10,00000 record.
it is posible to select first 10 , in next to start 11 to 20 ,in prev select 10 records and last 10 records in table to select query
means is posible to pagination of first,next,prev and last record and display the 10 records.
i am using sql server2005

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 09:07:44
Yes it is.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-07 : 09:08:33
quote:
Originally posted by amirs

hi
im my table their are 10,00000 record.
it is posible to select first 10 , in next to start 11 to 20 ,in prev select 10 records and last 10 records in table to select query
means is posible to pagination of first,next,prev and last record and display the 10 records.
i am using sql server2005


Search for Pagination+SQL Server in google

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-07 : 09:52:49
quote:
Originally posted by amirs

hi
im my table their are 10,00000 record.
it is posible to select first 10 , in next to start 11 to 20 ,in prev select 10 records and last 10 records in table to select query
means is posible to pagination of first,next,prev and last record and display the 10 records.
i am using sql server2005


its possible using row_number() function. just use a procedure like below

CREATE PROCEDURE GetPagedResults
@PageSize int=10,
@PageNo int=1
AS
SELECT columns
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY PK) AS Row_No,*
FROm YourTable
)t
WHERE t.RowNo BETWEEN (@PageNo-1)*@PageSize+1 AND @PageNo*@PageSize
GO


then call it like
GetPagedResults 10,1 --gets first page records
GetPagedResults 10,5 --gets 5th page records

for getting last page just use
DECLARE @LastPage int
SELECT @LastPage= CIELING(COUNT(1)/@PageSize) FROM YourTable
Go to Top of Page
   

- Advertisement -