我是靠谱客的博主 醉熏灯泡,这篇文章主要介绍mysql 存储过程循环表_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();

最后

以上就是醉熏灯泡最近收集整理的关于mysql 存储过程循环表_Mysql 存储过程 循环表的全部内容,更多相关mysql内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部