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循环的正确语法是什么?

回答:

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()

begin

declare v_max int unsigned default 1000;

declare v_counter int unsigned default 0;

truncate table foo;

start transaction;

while v_counter < v_max do

insert into foo (val) values ( floor(0 + (rand() * 65535)) );

set v_counter=v_counter+1;

end while;

commit;

end #

delimiter ;

call load_foo_test_data();

select * from foo order by id;

以上是 MySQL中的For循环示例 的全部内容, 来源链接: utcz.com/qa/436286.html

回到顶部