• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

瓜子与核桃

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

SQLserver 的分页存储过程

 

-- 1.建立修改学生数据的存储过程

-- 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+'%'
 
 -- 总页数计算(如果总记录数能被每页记录数除尽 则总页数=总记录数/每页大小 否则这就是 总记录数/每页大小+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

posted on 2020-06-22 19:55  瓜子与核桃  阅读(182)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3