通用分页存储过程

来源:互联网 发布:阿里云计算 北京地址 编辑:程序博客网 时间:2024/04/28 03:25
/*分页查找数据*/
createPROCEDURE [GetRecordSet]
@strSql varchar(8000),--查询sql,如select * from [user]
@PageIndex int,--查询当页号
@PageSize int--每页显示记录
,@recCount int out
AS
set nocount on
declare @p1 int
declare @currentPage int
set @currentPage = 0
declare @RowCount int
set @RowCount = 0
declare @PageCount int
set @PageCount = 0
print @strsql
--select @strsql
exec sp_cursoropen @p1 output,@strSql,@scrollopt=1,@ccopt=1,@rowcount=@rowCount output --得到总记录数
set @PageCount=ceiling(1.0*@rowCount/@pagesize) --得到总页数
set @currentPage=(@PageIndex-1)*@PageSize+1
--select @RowCount,@PageCount
set @recCount=@RowCount
exec sp_cursorfetch @p1,16,@currentPage,@PageSize
exec sp_cursorclose @p1
set nocount off