Sunday, October 24, 2010

Paging in SQL Server

declare @PageIndex int
declare @pageSize int
declare @startindex int
declare @endindex int



set @PageIndex = 1
set @pageSize = 10



set @startindex = @pageindex
set @endindex = @pagesize



if
(@pageindex > 1)
begin
set @endindex = @pageSize * @PageIndex
set @startindex = (@endindex - @pageSize ) + 1
end


Declare @totalrows int
select @totalrows =count(company_code) from tbl_CMT_Company
select *,@totalrows as totalRecords from
(
select Company_code,name,Arabic_name,
Row_number() over (order by Company_code) as rowindex
from tbl_CMT_Company
) as pageresult
where rowindex >=@startindex and rowindex <=@endindex

No comments: