MySQL 中的存储过程和游标
2015-06-15 17:46
706 查看
学习了存储过程的基本语法,但是郁闷的是用while循环遍历游标 出现死循环(真的是很郁闷),用repeat 和loop则没有任何问题。到现在为止还没有解决。
-- while循环 drop procedure if exists test_while; create procedure test_while(out k varchar(30)) begin declare i int default 5; set k=''; while(i>0) do set k=CONCAT(i,'-',k); set i=i-1; end while; end; call test_while(@k); select @k; -- 创建存储过程 将编号为奇数的商品的issale(是否打折 1:不打折 2:打折)设置为2(扩展:查询出id为偶数的商品信息) -- while循环 ----------- 有问题 (死循环)--未找到原因 drop procedure if exists setIssale; create PROCEDURE setIssale() begin declare v_id int; declare stop int default 0; declare product_cur cursor for select id from tb_product; -- DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = -1; declare continue handler for not found set stop=1; open product_cur; fetch product_cur into v_id; while (stop <> 1) DO if(v_id%2!=0) then update tb_product set Issale=2 where id=v_id; fetch product_cur into v_id; end if; end while; close product_cur; end; call setIssale(); select * from tb_product; -- repeat循环 ---- 这个是正确的 不是死循环 drop procedure if exists setIssale; create PROCEDURE setIssale() begin declare v_id int; declare done bool default false; declare product_cur cursor for select id from tb_product; declare continue handler for not found set done=true; open product_cur; repeat fetch product_cur into v_id; if(v_id%2!=0) then update tb_product set Issale=2 where id=v_id; end if; until done -- 此处不可以加";"分号,加分号会有语法错误 控制条件是当done为真时,跳出循环 end repeat; close product_cur; end; -- loop 循环 ---- 这个是正确的 不是死循环 drop procedure if exists setIssale; create PROCEDURE setIssale() begin declare v_id int; declare done bool default false; declare product_cur cursor for select id from tb_product; declare continue handler for not found set done=true; open product_cur; loop_label: loop -- 此处需要加一个空格,否则有语法错误 fetch product_cur into v_id; if(v_id%2!=0) then update tb_product set Issale=2 where id=v_id; end if; if done then leave loop_label; -- leave的作用是离开循环 end if; end loop; close product_cur; end; call setIssale(); select * from tb_product
-- 创建存储过程 将编号为奇数的商品的issale(是否打折 1:不打折 2:打折)设置为2 并且查询出id为偶数的商品信息 -- repeat循环 drop procedure if exists setIssale; create PROCEDURE setIssale() begin declare temp_id varchar(32) default '0'; declare v_id int; declare done bool default false; declare product_cur cursor for select id from tb_product; declare continue handler for not found set done=true; -- 创建一张临时表 drop table if exists tempidlist; create table tempidlist( id int ); open product_cur; repeat fetch product_cur into v_id; if(v_id%2!=0) then update tb_product set Issale=2 where id=v_id; else -- set temp_id=concat(temp_id,',',v_id); insert into tempidlist(id) values(v_id); end if; until done -- 此处不可以加";"分号,加分号会有语法错误 控制条件是当done为真时,跳出循环 end repeat; -- set temp_id=substring(temp_id,3); -- 因为MySQL 似乎不支持数组,所以为了保存偶数的id需要保存在一张临时表中。 -- select temp_id from dual; select * from tb_product where id in(select id from tempidlist ); close product_cur; end; call setIssale(); select * from tb_product;
相关文章推荐
- mysql 配置优化
- mysqldump的一些用法
- mysql配置(整理、备份)
- MySQL聚簇索引的使用介绍
- MySQL聚簇索引的使用介绍
- mysql-bin日志文件过大导致磁盘空间不足问题解决方法
- 全备与增备的优缺点比较
- Mysql 收集统计信息
- 查看mysql日志错误
- MySQL修改root密码的4种方法
- 修改mysql的my.ini配置文件存放位置
- CSV文件导入MySQL数据库方法
- 使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据
- MySQL: Lasy UNION
- MySQL 批量杀mysql线程
- 查看文章 mysql表注释和字段注释
- mysql 临时表
- MySQL和MsSQL实时自动数据同步
- Mysql 的事务隔离级别
- mysql入门总结