mysql 存储过程、游标及逐行处理的配合使用
2017-10-26 16:35
507 查看
1. 数据准备
2. 定义一个存储过程(供下一存储过程调用)
3. 存储过程、游标及逐行处理的配合使用
4. 调用存储过程及查询表aa02结果
5. 注意事项
这条语句定义了一个CONTINUE HANDLER,它是在条件出现时被执行的代码。这里,它指出当SQLSTATE '02000'出现时,SET done=1。SQLSTATE'02000'是一个未找到条件,当REPEAT由于没有更多的行供循环而不能继续时,出现这个条件。
DECLARE语句的次序 DECLARE语句的发布存在特定的次序。用DECLARE语句定义的局部变量必须在定义任意游标或句柄之前定义,而句柄必须在游标之后定义。不遵守此顺序将产生错误消息。
+----+------+--------+ | id | name | price1 | +----+------+--------+ | 1 | 大米 | 5 | | 2 | 鸡蛋 | 4.5 | | 3 | 苹果 | 6 | | 4 | 面粉 | 4 | | 5 | 小米 | 5.5 | +----+------+--------+
2. 定义一个存储过程(供下一存储过程调用)
delimiter // create procedure getPrice1( in id int, in addSelf boolean, out ototal decimal(6,2) ) comment '根据id及是否价格乘2来获取价格' begin -- 声明临时变量price declare price decimal(6,2); -- 根据id查询price1保存到临时变量price select price1 from aa01 where aa01.id = id into price; -- 判断是否需要乘2 if addSelf then select price * 2 into price; end if; -- 最后将临时变量赋值给输出变量 select price into ototal; end // delimiter ;
3. 存储过程、游标及逐行处理的配合使用
delimiter // create procedure copyIdAndDoublePrice1() comment '将原始表中id和price1*2结果赋值到表aa02中' begin -- 声明局部变量 declare done boolean default 0; declare tempId int; declare tempDoublePrice1 decimal(6,2); -- 声明游标 declare idIndex cursor for select id from aa01; -- 声明循环结束条件 declare continue handler for sqlstate '02000' set done = 1; -- 创建aa02表用于保存结果集 create table if not exists aa02( id int, doublePrice1 decimal(6,2) ); -- 打开游标 open idIndex; -- 循环所有行 repeat -- 获得当前循环的id fetch idIndex into tempId; -- 调用另一个存储过程获取结果 call getPrice1(tempId, 1, tempDoublePrice1); -- 将结果插入aa02表 insert into aa02(id, doublePrice1) values (tempId, tempDoublePrice1); -- 结束循环 until done end repeat; -- 关闭游标 close idIndex; end // delimiter ;
4. 调用存储过程及查询表aa02结果
call copyIdAndDoublePrice1(); select * from aa02;
+----+--------------+ | id | doublePrice1 | +----+--------------+ | 1 | 10 | | 2 | 9 | | 3 | 12 | | 4 | 8 | | 5 | 11 | | 5 | 11 | +----+--------------+
5. 注意事项
这条语句定义了一个CONTINUE HANDLER,它是在条件出现时被执行的代码。这里,它指出当SQLSTATE '02000'出现时,SET done=1。SQLSTATE'02000'是一个未找到条件,当REPEAT由于没有更多的行供循环而不能继续时,出现这个条件。
DECLARE语句的次序 DECLARE语句的发布存在特定的次序。用DECLARE语句定义的局部变量必须在定义任意游标或句柄之前定义,而句柄必须在游标之后定义。不遵守此顺序将产生错误消息。
相关文章推荐
- 在Oralce存储过程中使用游标来逐行处理数据示例并执行调试
- MySQL之21-29 重点: 视图,触发器,存储过程,游标,事务处理
- 使用游标(Cursors)将多行查询结果逐行处理
- mysql 存储过程 -- 游标的使用(备忘)
- mysql 使用游标进行删除操作的存储过程
- mysql 记录一次实际业务中使用到存储过程、游标
- oracle 存储过程,游标、异常处理,集合使用入门详解
- mysql存储过程 游标 循环使用介绍
- mysql 存储过程-----游标和处理程序
- mysql中使用存储过程,游标,定时任务
- MySql 使用存储过程和游标读取并更新数据
- mysql存储过程 游标使用
- mysql 存储过程 -- 游标的使用
- MySQL存储过程学习整理,游标使用,参数输入输出
- mysql存储过程 --游标的使用 取每行记录 (多字段)
- Mysql创建存储过程,使用游标Cursor循环更新
- Mysql使用存储过程和游标
- Mysql创建存储过程,使用游标Cursor循环更新
- mysql 使用游标进行删除操作的存储过程
- mysql中使用游标处理线上问题