MySQL存储过程(四)

Iterator迭代

MySQL存储过程到这基本就结束了,下来我们看看如何使用迭代?

MariaDB [db_test]> drop procedure if exists proc_iterator;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [db_test]> delimiter //
MariaDB [db_test]> create procedure proc_iterator()
    -> begin
    -> declare i int;
    -> set i = 0;
    -> loop_label:loop
    -> if i = 3 then set i = i+1;
    -> iterate  loop_label;
    -> end if;
    -> insert into tb_proc(num) values(i);
    -> set i = i+1;
    -> if i>=5 then leave loop_label;
    -> end if;
    -> end loop;
    -> end;//
Query OK, 0 rows affected (0.00 sec)

MariaDB [db_test]> delimiter ;

执行结果:
MySQL存储过程(四)
iterator的使用类似于continue,mysql存储过程这一部分到这里就结束了。我们可以根据所需数据设计相应的存储过程,使用call调用,避免我们做复杂查询。