Go----Universal Pagination code
CREATE PROCEDURE [dbo]. [Sp_datapager]
@pagesize int,--The size of each page
@pageindex int,--page number
Name of the @tablename varchar (Max),--table
@keycolumn varchar,---PRIMARY Key ID
@columns varchar,--to query the name of the Dequeue
@where varchar,---query criteria
@orderby varchar,---Sorting method
@recordcount int out--output parameter, not 0 returns the total number of records to query the table
As
declare @sql nvarchar (3000)
declare @rcsql nvarchar (1000)
Set @rcsql = ' Select @rc =count (*) from ' [email protected]
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + ' + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [email protected]+ ') '
if (@where! = ")
Begin
Set @rcsql = ' Select @rc =count (*) from ' [Email protected]+ ' where ' [email protected]
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [email protected]+ ' WHERE ' [email protected]+ ') and ' [email protected]
End
if (@orderby! = ")
Begin
if (@where! = ")
Begin
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [e-mail protected]+ ' where ' [email protected]+ ' ORDER BY ' [email protected]+ ') and ' +
@where + ' ORDER BY ' [email protected]
End
Else
Begin
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [e-mail protected]+ ' ORDER by ' [e-mail protected]+ ') ' + ' ORDER BY ' [email protected]
End
End
declare @param nvarchar (100)
Set @param = ' @rc int output '
EXEC sp_executesql @sql
EXEC sp_executesql @rcsql, @param, @[email protected] Output
SQL Universal Paging Code