当前位置:Gxlcms > mysql > Mysql向表中循环插入数据_MySQL

Mysql向表中循环插入数据_MySQL

时间:2021-07-01 10:21:17 帮助过:14人阅读

今天学习Mysql,做实验时想向一个标准插入1000行数据,在oracle中类似于这样

  1. <code class=" hljs sql">begin
  2. for i in 1..1000 loop
  3. insert ..
  4. end loop;
  5. end;
  6. /</code>

但是Mysql中不支持匿名块

百度了一下,方法如下:

首先设置delimiter

delimiter的作用:告诉解释器,这段命令是否已经结束了,mysql是否可以执行了
默认情况下,delimiter是‘;’但是当我们编写procedure时,如果是默认设置,那么一遇到‘;’,mysql就要执行,这是我们不希望看到的

所以我们手动设置delimiter为//

  1. <code class=" hljs cs">delimiter //
  2. create procedure per2()
  3. begin
  4. declare num int;
  5. set num=1;
  6. while num < 1000 do
  7. insert into per2(name) values(concat("fan", num));
  8. set num=num+1;
  9. end while;
  10. end
  11. //</code>

其中concat(“fan”, num),相当于oracle中fan||i的拼接效果,但是mysql不支持这样拼接

之后我们要调用这个procedure,才会插入数据

  1. <code class=" hljs haml">(mysql@localhost) [fandb]> call per2();
  2. -> //
  3. Query OK, 1 row affected (0.39 sec)</code>

由于我没有将delimiter’改回来,所以输入‘;’后并没有执行,还需要//

查看库中的procedure

  1. <code class=" hljs asciidoc">(mysql@localhost) [fandb]> select name from mysql.proc where db='fandb' and type='procedure';
  2. +------+
  3. | name |
  4. +------+
  5. | per2 |
  6. +------+
  7. 1 row in set (0.00 sec)</code>

查看创建代码

  1. <code class=" hljs vbnet">(mysql@localhost) [fandb]> show create procedure per2\G
  2. *************************** 1. row ***************************
  3. Procedure: per2
  4. sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
  5. Create Procedure: CREATE DEFINER=`mysql`@`localhost` PROCEDURE `per2`()
  6. begin
  7. declare num int;
  8. set num=1;
  9. while num < 1000 do
  10. insert into per2(name) values(concat("fan", num));
  11. set num=num+1;
  12. end while;
  13. end
  14. character_set_client: utf8
  15. collation_connection: utf8_general_ci
  16. Database Collation: utf8_general_ci
  17. 1 row in set (0.00 sec)</code>

其他使用方法

while ·· end while:

  1. <code class=" hljs oxygene">mysql > DELIMITER //
  2. mysql > CREATE PROCEDURE proc4()
  3. -> begin
  4. -> declare var int;
  5. -> set var=0;
  6. -> while var<6 do
  7. -> insert into t values(var);
  8. -> set var=var+1;
  9. -> end while;
  10. -> end;
  11. -> //</code>

? while 条件 do–循环体 endwhile

?repeat ·· end repeat:
它在执行操作后检查结果,而 while 则是执行前迚行检查。

  1. <code class=" hljs haml">mysql > DELIMITER //
  2. mysql > CREATE PROCEDURE proc5 ()
  3. -> begin
  4. -> declare v int;
  5. -> set v=0;
  6. -> repeat
  7. -> insert into t values(v);
  8. -> set v=v+1;
  9. -> until v>=5
  10. -> end repeat;
  11. -> end;
  12. -> //</code>

? repeat–循环体 until 循环条件 endrepeat;

loop ·· endloop:
loop 循环丌需要初始条件,这点和 while 循环相似,同时和 repeat 循环一样丌需要结束条
件, leave 诧句的意义是离开循环。

  1. <code class=" hljs haml">mysql > DELIMITER //
  2. mysql > CREATE PROCEDURE proc6 ()
  3. -> begin
  4. -> declare v int;
  5. -> set v=0;
  6. -> LOOP_LABLE:loop
  7. -> insert into t values(v);
  8. -> set v=v+1;
  9. -> if v >=5 then
  10. -> leave LOOP_LABLE;
  11. -> end if;
  12. -> end loop;
  13. -> end;
  14. -> //</code>

LABLES 标号:

标号可以用在 begin repeat while 戒者 loop 诧句前,诧句标号叧能在合法的诧句前面使用。
可以跳出循环,使运行指令达到复合诧句的最后一步。

人气教程排行