MySql存储过程循环语句使用
2022/5/29 2:20:00
本文主要是介绍MySql存储过程循环语句使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、while循环
1、格式
【标签】while 循环条件 do 循环体; end while 【标签】;
2、操作
案例一:满足某种条件终止循环
DROP PROCEDURE IF EXISTS proc1; delimiter $$ create procedure proc1(in in_count int) begin DECLARE i int DEFAULT 1; label:while i<=in_count do if i=6 then # 当i等于6就终止循环 LEAVE label; end if; INSERT into users(user_name,sex) VALUES(CONCAT('用户名','',i),CONCAT('性别','',i)); set i=i+1; end while label; end $$ delimiter ; call proc1(10);
案例二:满足某种条件跳过此处循环,并继续执行循环操作
DROP PROCEDURE IF EXISTS proc1; delimiter $$ create procedure proc1(in in_count int) begin DECLARE i int DEFAULT 0; label:while i<in_count do set i=i+1; if i=6 then # 当i等于6就跳出此次循环,继续执行 ITERATE label; end if; INSERT into users(user_name,sex) VALUES(CONCAT('用户名','',i),CONCAT('性别','',i)); end while label; end $$ delimiter ; call proc1(10);
二、repeat循环
1、格式
【标签】: repeat 循环体; until 条件表达式 end repeat 【标签】;
2、操作
DROP PROCEDURE IF EXISTS proc1; delimiter $$ create procedure proc1(in in_count int) begin DECLARE i int DEFAULT 1; label:repeat INSERT into users(user_name,sex) VALUES(CONCAT('用户名','',i),CONCAT('性别','',i)); set i=i+1; UNTIL i>in_count end repeat label; end $$ delimiter ; call proc1(10);
三、loop循环
1、格式
【标签】:loop 循环体; if 条件表达式 then leave 【标签】 end if; end loop;
2、操作
DROP PROCEDURE IF EXISTS proc1; delimiter $$ create procedure proc1(in in_count int) begin DECLARE i int DEFAULT 0; label:loop set i=i+1; if i=5 then LEAVE label; end if; INSERT into users(user_name,sex) VALUES(CONCAT('用户名','',i),CONCAT('性别','',i)); end loop; end $$ delimiter ; call proc1(10);
这篇关于MySql存储过程循环语句使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-25MySQL报错Duplicate entry '0' for key 'PRIMARY'
- 2024-05-29阿里 Canal 实时同步 MySQL 增量数据至 ClickHouse 数据库
- 2024-05-24在Linux下管理MySQL的大小写敏感性
- 2024-04-26MySQL查出时间比实际晚8小时的解决方案
- 2024-04-01JPA不识别MySQL的枚举类型
- 2024-03-30mysql数据库表卡死解决方法
- 2024-03-15MySQL多数据源笔记5-ShardingJDBC实战
- 2024-03-11natural join mysql
- 2024-03-11关于VS2017,VS2015 中利用 EF使用Mysql 不显示数据源问题解决方案
- 2024-02-26mysql 阿里云xb后缀备份文件恢复-icode9专业技术文章分享