1一、mssql的实现:
2/*----------------------------------------------------------------------------------
3 * description: 数据分页存储过程
4 * author: LiLF
5 * date: 2009-9-25
6---------------------------------------------------------------------------------*/
7
8ALTER procedure [dbo].[ExecSqlGetOnePage]
9@sqlstr varchar(8000),
10@currentpage int,
11@pagesize int
12as
13set nocount on
14declare @sumPageCount int
15declare @P1 int,
16 @rowcount int
17exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
18set @sumPageCount =ceiling(1.0*@rowcount/@pagesize)
19select @sumPageCount as 总页数
20select @rowcount as 记录总数
21
22 if (@currentpage>@sumPageCount)
23 set @currentpage = @sumPageCount
24 if (@currentpage < 1)
25 set @currentpage = 1
26
27set @currentpage=(@currentpage-1)*@pagesize+1
28exec sp_cursorfetch @P1,16,@currentpage,@pagesize
29exec sp_cursorclose @P1
30
31
32二、mysql的实现:
33/*----------------------------------------------------------------------------------
34 * description: 数据分页存储过程
35 * author: LiLF
36 * date: 2009-9-25
37---------------------------------------------------------------------------------*/
38#===drop procedure===============================
39DROP PROCEDURE IF EXISTS ExecSqlGetOnePage;
40#===create procedure========================
41CREATE 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 )
49BEGIN
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;
81END;
82#======invork procedure===================
83call ExecSqlGetOnePage('select * from system_framework',3,5);
84
2/*----------------------------------------------------------------------------------
3 * description: 数据分页存储过程
4 * author: LiLF
5 * date: 2009-9-25
6---------------------------------------------------------------------------------*/
7
8ALTER procedure [dbo].[ExecSqlGetOnePage]
9@sqlstr varchar(8000),
10@currentpage int,
11@pagesize int
12as
13set nocount on
14declare @sumPageCount int
15declare @P1 int,
16 @rowcount int
17exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
18set @sumPageCount =ceiling(1.0*@rowcount/@pagesize)
19select @sumPageCount as 总页数
20select @rowcount as 记录总数
21
22 if (@currentpage>@sumPageCount)
23 set @currentpage = @sumPageCount
24 if (@currentpage < 1)
25 set @currentpage = 1
26
27set @currentpage=(@currentpage-1)*@pagesize+1
28exec sp_cursorfetch @P1,16,@currentpage,@pagesize
29exec sp_cursorclose @P1
30
31
32二、mysql的实现:
33/*----------------------------------------------------------------------------------
34 * description: 数据分页存储过程
35 * author: LiLF
36 * date: 2009-9-25
37---------------------------------------------------------------------------------*/
38#===drop procedure===============================
39DROP PROCEDURE IF EXISTS ExecSqlGetOnePage;
40#===create procedure========================
41CREATE 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 )
49BEGIN
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;
81END;
82#======invork procedure===================
83call ExecSqlGetOnePage('select * from system_framework',3,5);
84