Mysql 存储过程 循环表

mysql 简单的使用存储过程进行遍历表;

use database1;
-- 删除存储过程
drop procedure if exists p_for_while; 
-- 创建存储过程
create procedure p_for_while()  
begin  
    declare i int;
        DECLARE uid int;
        DECLARE stop int DEFAULT 0;
        DECLARE cur CURSOR for(select id as uid from zspfsc_goods order by id asc);
             /*这把 游标 异常后 捕捉
                                 *        并设置 循环使用 变量 stop 为 null 跳出循环。
                */
        declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = null;
        set i = 1; 
                /*开游标*/
                OPEN cur;
                /*游标向下走一步,将查询出来的值付给定义的变量*/
                FETCH cur INTO uid;
                WHILE ( stop is not null) DO
                    update zspfsc_goods set order_num = i  where id = uid;
                    set i = i + 1;
                FETCH cur INTO uid;
        END WHILE;
        /*游标向下走一步*/
        CLOSE cur;
end;
-- 调用存储过程
call p_for_while();  

转载于:https://my.oschina.net/wugong/blog/1503304

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值