一些简单的调用以及语法规则这里就不在赘述,网上有许多例子。这里主要说说大家常用的游标加循环的嵌套使用。 首先先介绍循环的分类: (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;
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;
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