一、循环使用

mysql常见的三种循环方式:while、repeat和loop循环。还有一种goto,不推荐使用。

前提1、创建基本表结构

# 创建表结构
drop table if exists `test_table`;
create table `test_table`(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`modelid` varchar(50) COMMENT '字符主键',
`modelname` varchar(50) COMMENT '名称',
`desc` varchar(50) COMMENT '描述',
primary key (`id`)
) ENGINE=InnoDB charset=utf8 collate=utf8_bin;

1.1、while循环

delimiter //  #定义标识符为双斜杠
DROP PROCEDURE IF EXISTS my_procedure ; #如果存在 my_procedure 存储过程则删除
CREATE PROCEDURE my_procedure () #创建无参存储过程
BEGIN
DECLARE n INT DEFAULT 1 ; #申明变量
WHILE n <= 10 DO #结束循环的条件:
insert into test_table (modelid,modelname,`desc`)
value (n,CONCAT('name',n),'desc'); #处理语句
SET n = n + 1 ; #循环一次,i加一
END WHILE ; #结束while循环
select count(*) from test_table;
END
//             
delimiter ;
call my_procedure(); #调用存储过程

1.2、repeat

delimiter //                            #定义标识符为双斜杠
drop procedure if exists my_procedure; #如果存在test存储过程则删除
create procedure my_procedure() #创建无参存储过程,名称为test
begin
declare n int default 1; #申明变量
# set i = 0; #变量赋值
repeat
insert into test_table (modelid,modelname,`desc`)
value (n,CONCAT('name',n),'desc');
set n = n + 1; #循环一次,i加一
until n > 10 end repeat; #结束循环的条件: 当i大于10时跳出repeat循环
select count(*) from test_table; #查看test表数据
end
// #结束定义语句
call my_procedure(); #调用存储过程

1.3、loop

delimiter //                            #定义标识符为双斜杠
drop procedure if exists my_procedure; #如果存在test存储过程则删除
create procedure my_procedure() #创建无参存储过程,名称为test
begin
declare i int; #申明变量
set i = 1; #变量赋值
lp : loop #lp为循环体名,可随意 loop为关键字
insert into test_table (modelid,modelname,`desc`)
value (i,CONCAT('name',i),'desc');
set i = i + 1; #循环一次,i加一
if i > 10 then #结束循环的条件: 当i大于10时跳出loop循环
leave lp;
end if;
end loop;
select count(*) from test_table; #查看test表数据
end
// #结束定义语句
call my_procedure(); #调用存储过程
 

最新文章

  1. Leetcode-203 Remove Linked List Elements
  2. [转]十款提高开发效率的PHP编码工具
  3. Storm0.9.4安装 - OPEN 开发经验库
  4. SPOJ 7758. Growing Strings AC自动机DP
  5. HDU3047 Zjnu Stadium 带权并查集
  6. nginx服务器,php-fpm重启
  7. 网络通信 --&gt; 互联网协议(二)
  8. Java 枚举(enum) 详解7种常见的用法
  9. MVC传参数给js的时候 如果是数值 变量要进行一下转换才能正确识别 例如var aaa = parseInt(&#39;@Model.ClickIndex&#39;);
  10. WPFToolkit DataGrid 使用介绍zz
  11. Fastjson和Gson零碎总结
  12. 全向轮运动学与V-rep中全向移动机器人仿真
  13. Windows下使用pip安装python包是报错-UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xcb in position 0
  14. 如何做一个像btbook.net这样的搜片神器?
  15. windows下安装TA-Lib库
  16. Finding files on a *nix/Linux and sorting by size
  17. 聊一聊PV和并发、以及计算web服务器的数量的方法(转)
  18. WTL教程
  19. Codeforces - tag::graphs 大合集 [占坑]
  20. 【Linux资源管理】一款优秀的linux监控工具——nmon

热门文章

  1. 【Mybatis异常】 org.apache.ibatis.binding.BindingException: Parameter &#39;storeId&#39; not found. Available parameters are [form, param1]
  2. linux系统信息获取和上报
  3. MySQL/MariaDB数据库的Galera高可用性集群实战
  4. 在k8s集群部署SonarQube
  5. 路由器安全——破解wifi密码,同时中间人攻击
  6. Python常用标准库函数
  7. hive中执行hql或建表语句时,抛出Display all 459 possibilities? (y or n)错误的解决方法
  8. c++中关联容器set的使用
  9. Apache Shiro&lt;=1.2.4反序列化RCE漏洞
  10. WinDbg 图形界面功能(四)