zoukankan      html  css  js  c++  java
  • 分页存储过程

    网上有很多关于此存储过程的转载可是没有一个写有清晰的注释,为此研究后增加了注释。

    CREATE procedure main_table_pwqzc
    -----------------------------------------------------------------
    --定义变量
    -----------------------------------------------------------------
    (@pagesize int,--页面大小
    @pageindex int,--页的序号
    @docount bit,--是否浏览全部记录
    @this_id)/*对此存有疑问?????没有用,定义变量却没有指定数据类型*/
    as
    -----------------------------------------------------------------
    if(@docount=1)
    begin
    select count(id) from luntan where
    this_id=@this_id
    end
    else
    begin
    declare @indextable table(id int identity(1,1),nid int)/*定义临时表*/
    declare @PageLowerBound int
    declare @PageUpperBound int
    set @PageLowerBound=(@pageindex-1)*@pagesize/*最小值*/
    set @PageUpperBound=@PageLowerBound+@pagesize/*最大值*/
    set rowcount @PageUpperBound /*设置返回受影响的行数为最大值*/
    /*选择符合条件的ID插入到表@indextable*/
    insert into @indextable(nid) select id from luntan where
    this_id=@this_id order by reply_time desc
    /*从实际的表中选出ID相等的记录*/
    select a.* from luntan a,@indextable t where a.id=t.nid
    and t.id>@PageLowerBound and t.id<
    =@PageUpperBound order by t.id
    end
    GO
    ----------------------------------------------------------------------------
    /*存储过程会根据传入的参数@docount来确定是不是要返回所有要分页的记录总数
    特别是这两行

    ××××××××××××××××××××××××××××××
    set rowcount @PageUpperBound
    insert into @indextable(nid) select id from luntan where
    this_id=@this_id order by reply_time desc

    ××××××××××××××××××××××××××××××
    真的是妙不可言!!set rowcount @PageUpperBound当记录数达到@PageUpperBound时就会停止处理查询
    ,select id 只把id列取出放到临时表里,select a.* from luntan a,@indextable t where a.id=t.nid
    and t.id>@PageLowerBound and t.id<
    =@PageUpperBound order by t.id 而这句也只从表中取出所需要的记录,而不是所有的记录,结合起来,极大的提高了效率!!
    妙啊,真的妙!*/

    http://www.codeproject.com/KB/aspnet/PagingLarge.aspx:这位老外大哥总结的。

  • 相关阅读:
    HDU 5119 Happy Matt Friends(DP || 高斯消元)
    URAL 1698. Square Country 5(记忆化搜索)
    POJ 2546 Circular Area(两个圆相交的面积)
    URAL 1430. Crime and Punishment(数论)
    HDU 1111 Secret Code (DFS)
    HDU 1104 Remainder (BFS求最小步数 打印路径)
    URAL 1091. Tmutarakan Exams(容斥原理)
    PDO连接mysql8.0报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误
    swoole简易实时聊天
    centos安装netcat
  • 原文地址:https://www.cnblogs.com/zpino/p/1291299.html
Copyright © 2011-2022 走看看