您的位置:首页 > 数据库 > MySQL

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: