-- 2.建立根据班级Id和学生姓名模糊查询的分页存储过程,要求正确输出总记录数,总页数
-- (输入班学生姓名 计算总记录数 计算总页数)
-- (输入班学生姓名 计算总记录数 计算总页数)
-- @name 学生姓名模糊查询 @pagesize 每页大小 @currentpage 当前页 @totalcount 总记录数 @totalpage 总页数
create proc p_show(@stuName nvarchar(50),@pagesize int,@currentpage int out,@totalcount int out,@totalpage int out)
as
begin
-- 首先要对传进来的当前页进行判断,既不能小于1,也不能大于总页数
if @currentpage<1
begin
set @currentpage=1
end
-- 先计算总记录数
select @totalcount=COUNT(*) from student where stuName like ‘%‘+@stuName+‘%‘
create proc p_show(@stuName nvarchar(50),@pagesize int,@currentpage int out,@totalcount int out,@totalpage int out)
as
begin
-- 首先要对传进来的当前页进行判断,既不能小于1,也不能大于总页数
if @currentpage<1
begin
set @currentpage=1
end
-- 先计算总记录数
select @totalcount=COUNT(*) from student where stuName like ‘%‘+@stuName+‘%‘
-- 总页数计算(如果总记录数能被每页记录数除尽 则总页数=总记录数/每页大小 否则这就是 总记录数/每页大小+1)
if @totalcount % @pagesize = 0
set @totalpage=@totalcount / @pagesize
else
set @totalpage = @totalcount / @pagesize + 1
-- 在判断一下 如果当前页大于总页数,那么当前页 = 总页数
if @currentpage > @totalpage
set @currentpage = @totalpage
分页查询
select * from
(select a.id,a.stuName,a.birthDay,a.age,a.ClassId,b.ClassName,b.CCount, ROW_NUMBER() over (order by stuName)rn
from student a left join Class b on a.ClassId=b.ClassId where a.stuName like ‘%‘+@stuName+‘%‘)t1
where rn between (@currentpage-1) * @pagesize + 1 and @currentpage * @pagesize
end
go
--两个输出字段
declare @currentpage int,@totalCount int, @totalPage int
set @currentpage = 1
if @totalcount % @pagesize = 0
set @totalpage=@totalcount / @pagesize
else
set @totalpage = @totalcount / @pagesize + 1
-- 在判断一下 如果当前页大于总页数,那么当前页 = 总页数
if @currentpage > @totalpage
set @currentpage = @totalpage
分页查询
select * from
(select a.id,a.stuName,a.birthDay,a.age,a.ClassId,b.ClassName,b.CCount, ROW_NUMBER() over (order by stuName)rn
from student a left join Class b on a.ClassId=b.ClassId where a.stuName like ‘%‘+@stuName+‘%‘)t1
where rn between (@currentpage-1) * @pagesize + 1 and @currentpage * @pagesize
end
go
--两个输出字段
declare @currentpage int,@totalCount int, @totalPage int
set @currentpage = 1
调用分页存储过程
exec p_show ‘四‘,8,@currentpage out,@totalCount out, @totalPage out
select @currentpage,@totalCount,@totalPage
exec p_show ‘四‘,8,@currentpage out,@totalCount out, @totalPage out
select @currentpage,@totalCount,@totalPage