MySQL 中的存储过程和游标



学习了存储过程的基本语法,但是郁闷的是用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;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值