create
procedure `p_viewpage`(
$tablename varchar(200), --表名稱
$fieldlist varchar(2000), --顯示列名
$primarykey varchar(100), --主鍵
$where
varchar(1000), --查詢條件 不含'where'字元
$order
varchar(1000), --排序 不含'order by'字元,如id asc,userid desc,當@sorttype=3時生效
$sorttype int, --排序規則 1:正序asc 2:倒序desc 3:多列排序
$recordercount int, --記錄總數 0:會返回總記錄
$pagesize int, --分頁大小
$pageindex int, --頁索引
out $totalcount integer, --總記錄數
out $totalpagecount integer --總分頁數
) begin
if !(($tablename is
null
or $tablename='') or ($fieldlist is
null
or $fieldlist='') or ($primarykey is
null
or $primarykey='') or $sorttype < 1 or $sorttype >3 or $recordercount < 0 or $pagesize < 0 or $pageindex < 0) then
if ($where
isnull
or $where='') then
set @new_where1 = ' ' ;
set @new_where2 = ' where ' ;
else
set @new_where1 =concat(' where ',$where);
set @new_where2 =concat(' where ',$where,' and ');
endif;
if $order=''
or $sorttype = 1 or $sorttype = 2 then
if $sorttype = 1 then
set @new_order =concat(' order by ',$primarykey,' asc' );
endif;
if $sorttype = 2 then
set @new_order =concat(' order by ',$primarykey,' desc');
endif;
else
set @new_order =concat(' order by ',$order);
endif;
set @sqlcount = concat('select count(*) into @totalcount from ',$tablename,@new_where1);
set @sqlcount1 = concat('select ceiling((count(*)+0.0)/',$pagesize,') into @totalpagecount from ',$tablename,@new_where1);
if $recordercount = 0 then
prepare stmt1 from @sqlcount;
execute stmt1;
set $totalcount=@totalcount;
prepare stmt1 from @sqlcount1;
execute stmt1;
set $totalpagecount=@totalpagecount;
else
set $totalcount = $recordercount;
endif;
if $pageindex > ceiling(($totalcount+0.0)/$pagesize) then
set $pageindex = ceiling(($totalcount+0.0)/$pagesize);
endif;
if $pageindex = 0 or $pageindex = 1 then
set @sql=concat('select ',$fieldlist,' from ',$tablename,@new_where1,@new_order,' limit ',$pagesize);
else
if $sorttype = 1 then
set @sql=concat('select ',$fieldlist,' from ',$tablename,@new_where2,$primarykey,' > (select max(',$primarykey,') from (select ',$primarykey,' from ',$tablename,@new_where1,@new_order,' limit ',$pagesize*($pageindex-1),' ) as tmp) ',@new_order,' limit ',$pagesize);
endif;
if $sorttype = 2 then
set @sql=concat('select ',$fieldlist,' from ',$tablename,@new_where2,$primarykey,' < (select min(',$primarykey,') from (select ',$primarykey,' from ',$tablename,@new_where1,@new_order,' limit ',$pagesize*($pageindex-1),' ) as tmp) ',@new_order,' limit ',$pagesize);
endif;
if $sorttype = 3 then
if instr($order,',') > 0 then
set @sql=concat('select ',$fieldlist,' from ',$tablename,@new_where2,$primarykey,' not in (select ',$primarykey,' from (select ',$primarykey,' from ',$tablename,@new_where1,@new_order,' limit ',$pagesize*($pageindex-1),' ) a)',@new_order,' limit ',$pagesize);
else
set @new_order =concat(' order by ',$primarykey,' asc' );
set @sql=concat('select ',$fieldlist,' from ',$tablename,@new_where2,$primarykey,' > (select max(',$primarykey,') from (select ',$primarykey,' from ',$tablename,@new_where1,@new_order,' limit ',$pagesize*($pageindex-1),' ) as tmp) ',@new_order,' limit ',$pagesize);
endif;
endif;
endif;
prepare stmt2 from @sql;
execute stmt2;
endif;
end ;
mysql儲存過程 MySQL儲存過程
在本節中,您將逐步學習如何在mysql中編寫和開發儲存過程。首先,我們向您介紹儲存過程的概念,並討論何時使用它。然後,展示如何使用過程 的基本元素,如建立儲存過程的語句,if else,case,loop,儲存過程的引數。下面每個教程都包含了易於理解的示例和詳細的說明。如果您瀏覽並學習所有教程,您可...
mysql 儲存過程 mysql 儲存過程
建立 為建立儲存過程的結束標誌,使用delimiter 可更改標誌 格式create procedure begin sqlend create procedure myprocedure in param integer begin select from tb role where tb rol...
mysql 儲存過程 MySQL儲存過程
目錄 儲存過程 簡介是一組為了完成特定功能的sql語句集合 比傳統sql速度更快 執行效率更高 儲存過程的優點 執行一次後,會將生成的二進位制 駐留緩衝區,提高執行效率 sql語句加上控制語句的集合,靈活性高 在伺服器端儲存,客戶端呼叫時,降低網路負載 可多次重複被呼叫,可隨時修改,不影響客戶端呼叫...