1. 程式人生 > >Mysql儲存過程遊標加迴圈

Mysql儲存過程遊標加迴圈

Mysql的儲存過程是從版本5才開始支援的,所以目前一般使用的都可以用到儲存過程。今天分享下自己對於Mysql儲存過程的認識與瞭解。

一些簡單的呼叫以及語法規則這裡就不在贅述,網上有許多例子。這裡主要說說大家常用的遊標加迴圈的巢狀使用。

首先先介紹迴圈的分類:

(1)WHILE ... END WHILE  
(2)LOOP ... END LOOP  
(3)REPEAT ... END REPEAT  
(4)GOTO  
這裡有三種標準的迴圈方式:WHILE迴圈,LOOP迴圈以及REPEAT迴圈。還有一種非標準的迴圈方式:GOTO(不做介紹)。 
 
(1)WHILE ... END WHILE   
CREATE PROCEDURE p14()  
BEGIN  
  DECLARE v INT;  
  SET v = 0;  
  WHILE v < 5 DO  
    INSERT INTO t VALUES (v);  
    SET v = v + 1;  
  END WHILE;  
END;  
這是WHILE迴圈的方式。它跟IF語句相似,使用"SET v = 0;"語句使為了防止一個常見的錯誤,如果沒有初始化,預設變數值為NULL,而NULL和任何值操作結果都為NULL。


(2)REPEAT ... END REPEAT    
CREATE PROCEDURE p15 ()  
BEGIN  
  DECLARE v INT;  
  SET v = 0;  
  REPEAT  
    INSERT INTO t VALUES (v);  
    SET v = v + 1;  
    UNTIL v >= 5  
  END REPEAT;  
END;  
這是REPEAT迴圈的例子,功能和前面WHILE迴圈一樣。區別在於它在執行後檢查結果,而WHILE則是執行前檢查。類似於do while語句。注意到UNTIL語句後面沒有分號,在這裡可以不寫分號,當然你加上額外的分號更好。 
 
(3)LOOP ... END LOOP    
CREATE PROCEDURE p16 ()  
BEGIN  
  DECLARE v INT;  
  SET v = 0;  
  loop_label: LOOP  
    INSERT INTO t VALUES (v);  
    SET v = v + 1;  
    IF v >= 5 THEN  
      LEAVE loop_label;  
    END IF;  
  END LOOP;  
END; 
以上是LOOP迴圈的例子。LOOP迴圈不需要初始條件,這點和WHILE迴圈相似,同時它又和REPEAT迴圈一樣也不需要結束條件。 


ITERATE 迭代
 
如果目標是ITERATE(迭代)語句的話,就必須用到LEAVE語句 
 
CREATE PROCEDURE p20 ()  
BEGIN  
  DECLARE v INT;  
  SET v = 0;  
  loop_label: LOOP  
    IF v = 3 THEN  
      SET v = v + 1;  
      ITERATE loop_label;  
      END IF;  
    INSERT INTO t VALUES (v);  
    SET v = v + 1;  
    IF v >= 5 THEN   
      LEAVE loop_label;  
      END IF;  
    END LOOP;  
END;
ITERATE(迭代)語句和LEAVE語句一樣也是在迴圈內部的迴圈引用, 它有點像C語言中 的“Continue”,同樣它可以出現在複合語句中,引用複合語句標號,ITERATE(迭代)意思 是重新開始複合語句。 

以上是對於迴圈的幾種情況的介紹。接著就是介紹一個帶遊標的例子來詳細解釋。

begin

declare p_feeCode varchar(20);
declare p_feeName varchar(20);
declare p_billMoney float(12);
declare p_schemeMoney float(12);
declare allMoney float(10);
declare allUsedMoney float(10);
declare p_year varchar(50);
declare p_totalCompeleteRate float(12);
declare done int(10);
  declare flag int(2);
declare feeCodeCursor cursor for select feeCode from fee;//申明一個遊標變數


declare continue handler for not found set done=1;//申明迴圈結束的標誌位

set done=0;
select date_format(now(),'%Y') into p_year;

open feeCodeCursor;//開啟遊標
loop_label:LOOP
fetch feeCodeCursor into p_feeCode;//將遊標插入申明的變數
if done = 1 then
leave loop_label;
else
set flag = 0;
end if;
set p_schemeMoney=0;
set p_billMoney = 0;
select feeName into p_feeName from fee where feeCode=p_feeCode;
select sum(billMoney) into p_billMoney from bill_data where feeCode=p_feeCode and billDate like Concat(p_year, '%');
select schemeMoney into p_schemeMoney from total_scheme where feeCode=p_feeCode and schemeDate like Concat(p_year, '%')  limit 1;
if flag = 0 then
set done = 0;
end if;
if p_schemeMoney=0 then
set p_totalCompeleteRate=-1.0;
else
set p_totalCompeleteRate=(1.0*p_billMoney)/p_schemeMoney;
end if;
insert into total_summary values(p_feeCode,p_feeName,p_year,p_billMoney,p_totalCompeleteRate);
commit;
end LOOP;
close feeCodeCursor;//迴圈結束後需要關閉遊標

end

以上只是一個簡單的例子來說明如何使用,大家不需要關注具體業務邏輯,只需要關注的是其中標誌位值的修改情況,已經迴圈何時離開。以及遊標如何宣告,如何使用,至於裡面具體的操作和普通的sql語句沒有太大區別。此處是用一層迴圈,至於複雜業務需要需要兩層三層,可以繼續用同樣的方法繼續巢狀。以下給出雙層巢狀迴圈的,同樣大家只需要關注巢狀結構即可。

begin
declare p_projectID varchar(20);
declare p_projectName varchar(20);
declare p_feeCode varchar(20);
declare p_feeName varchar(20);
declare p_projectSchemeMoney float(10);
declare p_projectMoney float(10);
declare p_billMoney float(10);
declare p_year varchar(50);
declare p_projectFeeCompeleteRate float(10);
declare done1 int(10);
declare done2 int(10);
declare flag int(2);
declare feeCodeCursor cursor for select feeCode from fee;
declare continue handler for not found set done1=1;

set done1=0;
select date_format(now(),'%Y') into p_year;
delete from project_fee_summary;
open feeCodeCursor;


repeat //第一層巢狀開始
fetch feeCodeCursor into p_feeCode;
select feeName into p_feeName from fee where feeCode=p_feeCode;
if not done1 then
begin
declare projectIDCursor cursor for select projectID from project;
declare continue handler for not found set done2 = 1;
set done2=0;
open projectIDCursor;
loop_label:LOOP//第二層巢狀開始
fetch projectIDCursor into p_projectID;
select projectName into p_projectName from project where projectID=p_projectID;
if done2 = 1 then
leave loop_label;
else
set flag = 0;
end if;
if not done2 then
set p_projectSchemeMoney=0;
select sum(billMoney) into p_billMoney from bill_data where feeCode=p_feeCode and projectID=p_projectID and billDate like Concat(p_year, '%');
select projectSchemeMoney into p_projectSchemeMoney from project_scheme where feeCode=p_feeCode and projectID=p_projectID;
if flag = 0 then
set done2 = 0;
end if;
if p_projectSchemeMoney=0 then
set p_projectFeeCompeleteRate=-1;
else
set p_projectFeeCompeleteRate=(1.0*p_billMoney)/p_projectSchemeMoney;
end if;
insert into project_fee_summary values(p_feeCode,p_projectID,p_projectName,p_feeName,p_year,p_billMoney,p_projectFeeCompeleteRate,p_projectFeeCompeleteRate);
end if;
end LOOP;
select sum(billMoney) into p_projectMoney from bill_data where feeCode=p_feeCode and billDate like Concat(p_year, '%');
set p_projectFeeCompeleteRate=(1.0*p_projectMoney)/p_projectSchemeMoney;
insert into project_fee_summary values(p_feeCode,"total","total",p_feeName,p_year,p_projectMoney,p_projectFeeCompeleteRate,p_projectFeeCompeleteRate);
close projectIDCursor;
end;
end if;
until done1 
end repeat;
close feeCodeCursor;
end