存储过程分页(id>(max(id)) id<(min(id)))

USE [BBS]

GO

/****** Object:  StoredProcedure [dbo].[fenye2]    Script Date: 12/18/2010 13:42:38 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER proc [dbo].[fenye2]

(

@currentpage int, --当前页

@pagesize int,--页面有多少条记录

@key varchar(30),--表关键字(自增长)

@tablename varchar(30),--表 名字

@where varchar(50),--查询条件  等于NULL为空   有查询条件的(即不为空)不带where

@order varchar(1),-- 等于0  desc 降序  等于1 升序

@pagecount int,--有多少个页面

@str varchar(450) output--

)

as

 

begin

----------------------------------执行的SQL语句------------------------------------------------

 

declare @sql nvarchar(400),@orderby nvarchar(200)

declare @tempsqla varchar(200),@tempsqlb varchar(200)

 

 

-----------------------------------记录总数-----------------------------------------------------

 

declare @count int

 

 

-----------------------------------临时变量-----------------------------------------------------

 

 

declare @temp1 int,@temp2 int

 

--------------------------------------------------------------------------------------------------

set @tablename=' ' +@tablename+' '

 

set @key=' '+@key+'  '

 

-----------------------------------给@count赋值,声明@count是output类型-----------------------------------------------------

 

if @where='null'

 

set @sql='select @count=count(*) from '+@tablename

 

else

 

set @sql='select @count=count(*) from '+@tablename+' where '+@where

 

exec sp_executesql @sql,N'@count int out',@count out

 

---------------------------------设置升序、降序------------------------------------------------------------

 

if @order='0'

 

set @orderby=' order by '+@key+' desc'

 

else

 

set @orderby=' order by '+@key

 

 

------------------------------------总页数---------------------------------------------------

 

if(@count%@pagesize)=0

 

set @pagecount=@count/@pagesize

 

else

 

set @pagecount=@count/@pagesize+1

 

 

------------------------------判断当前页是否显示异常--------------------------------------------------------------

 

if @currentpage>@pagecount

 

set @currentpage=@pagecount

 

if @currentpage<1

 

set @currentpage=1

 

 

---------------------------------设置已经完成,开始排序-----------------------------------------------------------------------

 

 

 

---------------------------------显示第一页数据-----------------------------------------------------------------------

 

if @currentpage=1

 

begin

 

 

if @where='null'

 

set @where=' '

 

else

 

set @where=' where '+@where

 

set @sql='select top '+STR(@pagesize)+' * from '+@tablename+@where+@orderby

 

end

 

else

 

begin

 

 

 /* ---------------desc---------------------- 

       *@temp1表示前面的记录

       *@temp2表示后面的记录

       *假设一共77个记录,每次取10个。取67~58(第2页),去掉前面的57(1~57)个和后面的10个(77~66)

       */

 

if @order='0'

 

begin

 

 

set @temp1=@count-@currentpage*@pagesize  -- temp1= 57  3,44

 

 

 

set @temp2=(@currentpage-1)*@pagesize  --10  temp2=10  3,20   3  >44  <58   4  >33 <30

 

if @where='null'

 

begin

if @temp1<=0

begin

set @tempsqla='0'

 

set @tempsqlb='select MIN('+(@key)+') from (select top '+STR(@temp2)+' '+@key+' from '+@tablename+@orderby+' ) as T2'

end

else

 

begin

set @tempsqla='select MAX('+(@key)+') from (select top '+STR(@temp1)+' '+@key+' from '+@tablename+' order by '+@key+' ) as T1'

 

set @tempsqlb='select MIN('+(@key)+') from (select top '+STR(@temp2)+' '+@key+' from '+@tablename+@orderby+' ) as T2'

 

end

 

end

 

else

 

begin

if @temp1<=0

begin

set @tempsqla='0'

 

set @tempsqlb='select MIN('+(@key)+') from (select top '+STR(@temp2)+' '+@key+' from '+@tablename+' where '+@where+@orderby+' ) as T2'

end

else

begin

set @tempsqla='select MAX('+(@key)+') from (select top '+STR(@temp1)+' '+@key+' from '+@tablename+' where '+@where+' order by '+@key+' ) as T1'

 

set @tempsqlb='select MIN('+(@key)+') from (select top '+STR(@temp2)+' '+@key+' from '+@tablename+' where '+@where+@orderby+' ) as T2'

end

end

 

 

 

set @sql='select top '+STR(@pagesize)+' * from '+@tablename+' where '+@key+' <'

 

set @sql=@sql+' ( '+@tempsqlb+' ) and '

 

set @sql=@sql+@key+' > ( '+@tempsqla+' )'

 

 

 

 

 

 

if @where='null'

 

 

set @sql=@sql+@orderby

 

else

 

set @sql=@sql+' and '+@where+@orderby

 

end

 

else

 

begin

 

set @temp1=(@currentpage-1)*@pagesize

 

if @where='null'

 

set @tempsqla='select MAX('+(@key)+') from (select top '+STR(@temp1)+' '+@key+' from '+@tablename+@orderby+' ) as T3'

 

else

 

set @tempsqla='select MAX('+(@key)+') from (select top '+STR(@temp1)+' '+@key+' from '+@tablename+' where '+@where+@orderby+' ) as T3'

 

set @sql='select top '+STR(@pagesize)+' * from '+@tablename+' where '+@key+' >'

 

set @sql=@sql+' ( '+@tempsqla+' ) '

 

if @where='null'

 

set @sql=@sql+@orderby

 

else

 

set @sql=@sql+' and '+@where+@orderby

 

end

 

-------------------------------结束---------------------------------------------

end

 

set @str=@sql

 

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<html> <head> <title>人员管理界面</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>员工列表 —— 显示所有员工信息</small> </h1> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>电话号</th> <th>地址</th> <th>职位</th> <th>职位编号</th> <th>状态</th> <th>证件照</th> </tr> </thead> <tbody> <c:forEach items="${employees}" var="employees"> <tr> <td>${employees.id}</td> <td>${employees.name}</td> <td>${employees.phonenumber}</td> <td>${employees.address}</td> <td>${employees.job}</td> <td>${employees.jobid}</td> <td>${employees.state}</td> <td><img src="${pageContext.request.contextPath}/images/${employees.pic}" style="width: 100px; height: 50px"></td> <td> <a href="${pageContext.request.contextPath}/toupdate/${employees.id}">更改</a> | <a href="${pageContext.request.contextPath}/delete/${employees.id}" onclick="return confirm('确定要删除吗?')">删除</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> </div> </body> </html>给这个界面的表格实现一个分页的功能,每页的最大数目为5
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值