测试表结构如下,使用存储过程的三种循环结构向表中插入数据。
create table tb
(
type varchar(20) not null,
name varchar(255) not null,
id varchar(20) not null,
primary key (type, id),
constraint index1
unique (name)
);
三种循环结构为: loop……end loop while……do……end while repeat……until…end repeat
create procedure insertData()
begin
declare i int default 0;
loop_label:
loop
insert into tb values (i, concat('test', i), i);
set i = i + 1;
if i > 100 then
leave loop_label;
end if;
end loop;
end;
create procedure insertData()
begin
declare i int default 0;
while i < 100
do
insert into tb values (i, concat('test', i), i);
set i = i + 1;
end while;
end;
create procedure insertData()
begin
declare i int default 0;
repeat
insert into tb values (i, concat('test', i), i);
set i = i + 1;
until i > 100
end repeat;
end;
Q.E.D.