create PROCEDURE dbo.Pageation
(
@PageSize int, --每页记录
@PageIndex int, --当前页数,1开始
@Condition varchar(8000), --查询条件,包括and,where,必须有一个条件如where 2>1
@TheTable varchar(8000), --表名
@SelectField varchar(8000), --要选择的字段
@OrderBy varchar (8000), --OrderBy字句,包括order
@TableID varchar (8000) -- --table主键
)
AS
begin
declare @Sql varchar(8000)
--返回记录
set @Sql='select top ' + cast(@PageSize as varchar(10)) + ' ' + @SelectField + ' from ' + @TheTable + ' ' + @Condition + ' and '
+ @TableID +' not in (select top ' +CAST((@PageSize*(@PageIndex-1)) AS VARCHAR(20))+' ' +@TableID+' from ' + @TheTable + ' ' + @Condition
+ ' ' + @OrderBy +') ' + @OrderBy
exec(@sql)
--返回总数
set @Sql='select count(' + @TableID + ') from ' + @TheTable +' ' + @Condition
exec(@sql)
end