【精选】mysql存储过程游标循环使用介绍

上传人:豆浆 文档编号:891632 上传时间:2017-05-20 格式:DOC 页数:9 大小:39.50KB
返回 下载 相关 举报
【精选】mysql存储过程游标循环使用介绍_第1页
第1页 / 共9页
【精选】mysql存储过程游标循环使用介绍_第2页
第2页 / 共9页
【精选】mysql存储过程游标循环使用介绍_第3页
第3页 / 共9页
【精选】mysql存储过程游标循环使用介绍_第4页
第4页 / 共9页
【精选】mysql存储过程游标循环使用介绍_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《【精选】mysql存储过程游标循环使用介绍》由会员分享,可在线阅读,更多相关《【精选】mysql存储过程游标循环使用介绍(9页珍藏版)》请在金锄头文库上搜索。

1、mysql 存储过程 游标 循环使用介绍今天分享下自己对于 Mysql 存储过程的认识与了解,这里主要说说大家常用的游标加循环的嵌套使用Mysql 的存储过程是从版本 5 才开始支持的,所以目前一般使用的都可以用到存储过程。今天分享下自己对于 Mysql 存储过程的认识与了解。一些简单的调用以及语法规则这里就不在赘述,网上有许多例子。这里主要说说大家常用的游标加循环的嵌套使用。 首先先介绍循环的分类: (1)WHILE . END WHILE (2)LOOP . END LOOP (3)REPEAT . END REPEAT (4)GOTO 这里有三种标准的循环方式:WHILE 循环,LOOP

2、 循环以及 REPEAT 循环。还有一种非标准的循环方式:GOTO(不做介绍)。 (1)WHILE . END WHILE 复制代码 代码如下:CREATE PROCEDURE p14() BEGIN DECLARE v INT; SET v = 0; WHILE v = 5 END REPEAT; END; 这是 REPEAT 循环的例子,功能和前面 WHILE 循环一样。区别在于它在执行后检查结果,而WHILE 则是执行前检查。类似于 do while 语句。注意到 UNTIL 语句后面没有分号,在这里可以不写分号,当然你加上额外的分号更好。 (3)LOOP . END LOOP 复制代码

3、 代码如下: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 语句 复制代码

4、代码如下: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”,同样它可以出现

5、在复合语句中,引用复合语句标号,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 varch

6、ar(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;/打开游标 l

7、oop_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_

8、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

9、*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 语句没有太大区别。此处是用一层循环,至于

10、复杂业务需要需要两层三层,可以继续用同样的方法继续嵌套。以下给出双层嵌套循环的,同样大家只需要关注嵌套结构即可。 复制代码 代码如下: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 flo

11、at(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)

12、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

13、 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_projectS

14、chemeMoney=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 d

15、one2 = 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_pr

16、ojectFeeCompeleteRate); 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 en

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号