createprocedure`mysqltestuser_select_pageable`(
_whereclausevarchar(2000),--查詢條件
_orderbyvarchar(2000),--排序條件
_pagesizeint,--每頁記錄數
_pageindexint,--當前頁碼
_docountbit--標誌:統計資料/輸出資料
)notdeterministic
sqlsecuritydefiner
comment''
begin
--定義key欄位臨時表
droptableifexists_temptable_keyid;--刪除臨時表,如果存在
createtemporarytable_temptable_keyid
(useridint
)type=heap;
--構建動態的sql,輸出關鍵字key的id集合
--查詢條件
set@sql='selectuseridfrommysqltestuser';
if(_whereclauseisnotnull)and(_whereclause<>'')then
set@sql=concat(@sql,'where',_whereclause);
endif;
if(_orderbyisnotnull)and(_orderby<>'')then
set@sql=concat(@sql,'orderby',_orderby);
endif;
--準備id記錄插入到臨時表
set@sql=concat('insertinto_temptable_keyid(userid)',@sql);
preparestmtfrom@sql;
executestmt;
deallocatepreparestmt;
--key的id集合[end]
--下面是輸出
if(_docount=1)then--統計
begin
selectcount(*)asrecordcountfrom_temptable_keyid;
end;
else--輸出記錄集
begin
--計算記錄的起點位置
set@startpoint=ifnull((_pageindex-1)*_pagesize,0);
set@sql='selecta.*
frommysqltestusera
innerjoin_temptable_keyidb
ona.userid=b.userid';
set@sql=concat(@sql,"limit",@startpoint,",",_pagesize);
preparestmtfrom@sql;
executestmt;
deallocatepreparestmt;
end;
endif;
droptable_temptable_keyid;
end;
drop procedure if exists pr_pager;
create procedure pr_pager(
in p_table_name varchar(1024), /*表名*/
in p_fields varchar(1024), /*查詢字段*/
in p_page_size int, /*每頁記錄數*/
in p_page_now int, /*當前頁*/
in p_order_string varchar(128), /*排序條件(包含order關鍵字,可為空)*/
in p_where_string varchar(1024), /*where條件(包含where關鍵字,可為空)*/
out p_out_rows int /*輸出記錄總數*/
)not deterministic
sql security definer
comment '分頁儲存過程'
begin
/*定義變數*/
declare m_begin_row int default0;
declare m_limit_string char(64);
/*構造語句*/
set m_begin_row = (p_page_now -1) * p_page_size;
set m_limit_string = concat(' limit ', m_begin_row, ', ', p_page_size);
set @count_string = concat('select count(*) into @rows_total from ', p_table_name, ' ', p_where_string);
set @main_string = concat('select ', p_fields, ' from ', p_table_name, ' ', p_where_string, ' ', p_order_string, m_limit_string);
/*預處理*/
prepare count_stmt from @count_string;
execute count_stmt;
deallocate prepare count_stmt;
set p_out_rows = @rows_total;
prepare main_stmt from @main_string;
execute main_stmt;
deallocate prepare main_stmt;
end;
mysql儲存過程示例
create procedure pro cancel order in orderid int in userid int out resultstatus int comment 取消商品訂單 begin 引數說明 orderid 訂單id extras 擴充套件記錄 異常處理 declare ...
MySQL儲存過程示例
mysql儲存過程 自定義結束符 delimiter 如果存在同名的儲存過程就刪除 drop procedure if exists praddblack 建立儲存過程 create procedure praddblack in n int begin while n 999 do insert ...
mysql 儲存過程示例
在mysql的test庫中執行如下sql table structure for user drop table if exists user create table user id int 11 unsigned not null auto increment,tinyint 1 not nul...