如果你正在mysql教程 5以上版本,我要告訴你這裡有三款 mysql 分頁預存程序執行個體哦,預存程序是mysql 5.0以後才支援的,現在看看這款預存程序吧,看一款簡單預存程序
*mssql預存程序
*/
create definer=`root`@`localhost` procedure `getrecordasp教程age`(
in tbname varchar(100),
fldname varchar(100),
pagesize int,
pageindex int,
ordertype int,
strwhere varchar(2000)
)
begin
declare beginrow int;
declare sqlstr varchar(1000);
declare limittemp varchar(1000);
declare ordertemp varchar(1000);
set beginrow = (pageindex-1)*pagesize;
set sqlstr = concat('select * from ',tbname);
set limittemp = concat(' limit ',beginrow,',',pagesize);
set ordertemp = concat(' order by ',fldname);
if ordertype = 0 then
set ordertemp = concat(ordertemp,' asc ');
else
set ordertemp = concat(ordertemp,' desc ');
end if;
set @sqlstring = concat(sqlstr,' ',strwhere,ordertemp,limittemp);
prepare sqlstmt from @sqlstring;
execute sqlstmt;
deallocate prepare sqlstmt;
end
create definer=`root`@`localhost` procedure `getrecordcount`(
in tbname varchar(20),
in strwhere varchar(20)
)
begin
if strwhere!="" then
set @strsql=concat('select count(*) from ',tbname,' where ',strwhere);
else
set @strsql=concat('select count(*) from ',tbname);
end if;
prepare sqlstmt from @strsql;
execute sqlstmt;
deallocate prepare sqlstmt;
end
這是一款高手分享的他的預存程序
create procedure `mysqltestuser_select_pageable`(
_whereclause varchar(2000), -- 尋找條件
_orderby varchar(2000), -- 排序條件
_pagesize int , -- 每頁記錄數
_pageindex int , -- 當前頁碼
_docount bit -- 標誌:統計資料/輸出資料
)
not deterministic
sql security definer
comment ' '
begin
-- 定義key欄位暫存資料表
drop table if exists _temptable_keyid; -- 刪除暫存資料表,如果存在
create temporary table _temptable_keyid
(
userid int
)type=heap;
-- 構建動態sql,輸出關鍵字key的id集合
-- 尋找條件
set @sql = 'select userid from mysqltestuser ';
if (_whereclause is not null) and (_whereclause <> ' ') then
set @sql= concat(@sql, ' where ' ,_whereclause);
end if;
if (_orderby is not null) and (_orderby <> ' ') then
set @sql= concat( @sql , ' order by ' , _orderby);
end if;
-- 準備id記錄插入到暫存資料表
set @sql=concat( 'insert into _temptable_keyid(userid) ', @sql);
prepare stmt from @sql;
execute stmt ;
deallocate prepare stmt;
-- key的id集合 [end]
-- 下面是輸出
if (_docount=1) then -- 統計
begin
select count(*) as recordcount from _temptable_keyid;
end;
else -- 輸出記錄集
begin
-- 計算記錄的起點位置
set @startpoint = ifnull((_pageindex-1)*_pagesize,0);
set @sql= ' select a.*
from mysqltestuser a
inner join _temptable_keyid b
on a.userid =b.userid ';
set @sql=concat(@sql, " limit ",@startpoint, " , ",_pagesize);
prepare stmt from @sql;
execute stmt ;
deallocate prepare stmt;
end;
end if;
drop table _temptable_keyid;
end;
下面是mysqltestuser表的ddl:
create table `mysqltestuser` (
`userid` int(11) not null auto_increment,
`name` varchar(50) default null,
`chinesename` varchar(50) default null,
`registerdatetime` datetime default null,
`jf` decimal(20,2) default null,
`description` longtext,
primary key (`userid`)
) engine=innodb default charset=gb2312;
插入些資料:
insert into `mysqltestuser` (`userid`, `name`, `chinesename`, `registerdatetime`, `jf`, `description`) values
(1, 'xuu1 ', 'www.111cn.net', '2007-03-29 12:54:41 ',1.5, 'description1 '),