Search This Blog

Tuesday, April 19, 2011

paging in DataList by stored procedure in SQL

CREATE procedure getQueryPageitems
 @totalItemCount int,       
 @pageNumber int,       
 @pageSize int       
as       
declare @numberOfItemToSelectForPage int       
declare @totalItemsToSel int   
declare @query nvarchar(4000)       
if(@pageSize*@pageNumber <= @totalItemCount)       
begin        
 set @numberOfItemToSelectForPage = @pageSize       
 set @totalItemsToSel = @pageSize*@pageNumber       
end       
else       
begin        
 set @numberOfItemToSelectForPage =  @totalItemCount - (@pageSize*(@pageNumber -1))      
 set @totalItemsToSel = @totalItemCount       
end   
begin
select  @query = 'select * from(select top ' + cast(@numberOfItemToSelectForPage as varchar(10))       
 + ' * from (select top ' + cast(@totalItemsToSel as varchar(10))        
+ ' * from (select * from  Query where Leedflag=1) tbl1       
    order by EntryDate desc)tabl2   order by EntryDate asc)tbl3   order by EntryDate desc'
end  
--print @query       
exec (@query)

GO

2 comments :

Strechmind....... said...
This comment has been removed by the author.
Strechmind....... said...

good thinking please provide cs part of this....