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:这位老外大哥总结的。

  • 相关阅读:
    POJ2155 Matrix 【二维线段树】
    BZOJ4785 [Zjoi2017]树状数组 【二维线段树 + 标记永久化】
    B1027 打印沙漏
    Tomcat无法成功启动——双击startup.bat闪退
    MySQL在cmd命令行查看端口号
    1009 说反话(类似回文字符串)
    除基取余法,
    日期差值
    怎么把VS里的scanf_s换成scanf
    联想小新潮怎么修改fn热键以及怎么进入bios状态
  • 原文地址:https://www.cnblogs.com/zpino/p/1291299.html
Copyright © 2011-2022 走看看