MySQL中的For循环示例

在MySQL中,我有一个带有For循环的存储过程:


DELIMITER $$  

CREATE PROCEDURE ABC()


   BEGIN

      DECLARE a INT Default 0 ;

      simple_loop: LOOP

         SET a=a+1;

         select a;

         IF a=5 THEN

            LEAVE simple_loop;

         END IF;

   END LOOP simple_loop;

END $$

它总是打印1。MySQL for循环的正确语法是什么?


慕森卡
浏览 8038回答 3
3回答

小怪兽爱吃肉

drop table if exists foo;create table foo(id int unsigned not null auto_increment primary key,val smallint unsigned not null default 0)engine=innodb;drop procedure if exists load_foo_test_data;delimiter #create procedure load_foo_test_data()begindeclare v_max int unsigned default 1000;declare v_counter int unsigned default 0;&nbsp; truncate table foo;&nbsp; start transaction;&nbsp; while v_counter < v_max do&nbsp; &nbsp; insert into foo (val) values ( floor(0 + (rand() * 65535)) );&nbsp; &nbsp; set v_counter=v_counter+1;&nbsp; end while;&nbsp; commit;end #delimiter ;call load_foo_test_data();select * from foo order by id;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL