Monday, August 10, 2009

Use Custom Paging in Asp.Net With Page Size (Faster Performance)

/*
Author : Milind Kansagara
SP Name : usp_Quoter_ListAll
Date : 15 Feb, 2007
Desc : Select all Quoter's list.

*/
CREATE procedure usp_Quoter_ListAll
(
@SortBy varchar(50)=null,
@SortOrder varchar(5)=null,
@PageNo int=null,
@PageSize int=null
)
as
begin
set nocount on
declare @strSql nvarchar(2000)
declare @strSqlCnt nvarchar(200)

set @strSqlCnt = 'select count(*) from quoters where 1=1 '

if @SortBy is null and @SortOrder is null and @PageNo is null and @PageSize is null
begin
set @strSql = 'select qtnum, name, quotes_posted, mod_pts, blocked, email_address from quoters'
end
else if @SortBy is not null and @SortOrder is not null and @PageNo is null and @PageSize is null
begin
set @strSql = 'select qtnum, name, quotes_posted, mod_pts, blocked, email_address from quoters order by ' + @SortBy + ' ' + @SortOrder
end
else if @SortBy is not null and @SortOrder is not null and @PageNo is not null and @PageSize is not null
begin
set @strSql = 'select top ' + cast(@PageSize as varchar(5) ) + ' qtnum, name, quotes_posted, mod_pts, blocked, email_address from quoters where qtnum not in ( select top ' + cast(@PageNo * @PageSize as varchar(10) ) + ' qtnum from quoters order by ' + @SortBy + ' ' + @SortOrder + ') order by ' + @SortBy + ' ' + @SortOrder
end

exec sp_executesql @strSql
exec sp_executesql @strSqlCnt

end
GO

No comments: