程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> mysql存儲過程 游標 循環使用介紹

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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved