数据库分页算法

 1 一、mssql的实现:
 2 /**/ /*----------------------------------------------------------------------------------
 3 * description: 数据分页存储过程
 4 * author:  LiLF
 5 * date:  2009-9-25
 6---------------------------------------------------------------------------------*/

 7
 8 ALTER    procedure   [ dbo ] . [ ExecSqlGetOnePage ]
 9 @sqlstr   varchar ( 8000 ), 
10 @currentpage   int
11 @pagesize   int  
12 as
13 set  nocount  on
14 declare   @sumPageCount   int  
15 declare   @P1   int
16   @rowcount   int
17 exec  sp_cursoropen  @P1  output, @sqlstr , @scrollopt = 1 , @ccopt = 1 , @rowcount = @rowcount  output
18 set   @sumPageCount   = ceiling ( 1.0 * @rowcount / @pagesize )
19 select   @sumPageCount   as  总页数
20 select   @rowcount   as  记录总数
21
22              if  ( @currentpage > @sumPageCount )
23                  set   @currentpage   =   @sumPageCount
24              if  ( @currentpage   <   1 )
25                  set   @currentpage   =   1
26
27 set   @currentpage = ( @currentpage - 1 ) * @pagesize + 1
28 exec  sp_cursorfetch  @P1 , 16 , @currentpage , @pagesize  
29 exec  sp_cursorclose  @P1
30
31
32 二、mysql的实现:
33 /**/ /*----------------------------------------------------------------------------------
34 * description: 数据分页存储过程
35 * author:  LiLF
36 * date:  2009-9-25
37---------------------------------------------------------------------------------*/

38 # === drop   procedure ===============================
39 DROP   PROCEDURE   IF   EXISTS  ExecSqlGetOnePage;
40 # === create   procedure ========================
41 CREATE  DEFINER = `root`@`localhost`  PROCEDURE  `ExecSqlGetOnePage`(
42      /**/ /*query string*/
43  $sqlstr  varchar ( 8000 ),
44      /**/ /*Page index*/
45     $pageIndex  int ,
46      /**/ /*Page Size*/
47     $pageSize  int  
48   )
49 BEGIN   
50
51   DECLARE  $sumPageCount  int ;
52     # === return  a empty  table ======
57     
58      set   @rowcount = 0 ;
59      set   @sqlcounts = concat( ' select COUNT(*) into @rowcount from ( ' ,$sqlstr, ' ) as a ' );
60      PREPARE  stmt1  FROM   @sqlcounts ;  
61      EXECUTE  stmt1;
62      DEALLOCATE   PREPARE  stmt1;
63  
64      set  $sumPageCount = ceiling ( 1.0 * @rowcount / $pagesize);
65      # ==== return  total pages ======
66      select  $sumPageCount  as   ' totalPages ' ;
67     # ==== return  total records =====
68   select   @rowcount   as   ' totalRows ' ;
69
70      if  ($pageIndex > $sumPageCount)  THEN
71          set  $pageIndex  =  $sumPageCount;
72      end   if ;
73      if  ($pageIndex  <   1 then
74          set  $pageIndex  =   1 ;
75      end   if ;
76     # ==== return   current  page records =====        
77   set   @strSQL = concat($sqlstr, '  LIMIT   ' ,($pageIndex - 1 ) * $pageSize, ' , ' ,$pageSize);   
78      PREPARE  stmt1  FROM   @strSQL ;  
79      EXECUTE  stmt1;
80      DEALLOCATE   PREPARE  stmt1;
81 END ;
82 # ====== invork  procedure ===================
83 call ExecSqlGetOnePage( ' select * from system_framework ' , 3 , 5 );
84

你可能感兴趣的:(数据库分页算法)