SQL Server分页的存储过程

use 数据库名
go
create proc 存储过程名
@index int,  ---索引页
@size int,   ---每页的条数
@totalCount int output,  ---多少条数据
@totalPage int output   ---多少页数
as
select @totalCount= count(*) from newsmodels ---查询有多少条数据
set @totalPage=@totalCount/@size  --查询多少页数
if(@totalCount%@size>0)   --判断是否需要加1
set @totalPage+=1
select top(@size) * from 表名where nid not in (select top((@index-1)*@size) nid from 表名) --执行结果
go
declare @tcount int,@tpage int  ---定义两个变量来接受总条数和总页数
exec 存储过程名 每页显示显示行数,显示第几页,@tcount output,@tpage output   ---执行存储过程
select 总条数=@tcount,总页数=@tpage
select * from 表名

posted @ 2018-08-22 10:51  月下逢  阅读(123)  评论(0)    收藏  举报