时间:2021-07-01 10:21:17 帮助过:18人阅读
上面我们使用if then条件判断,下面来看使用case when来完成更多的条件:
drop procedure if exists t1;
create procedure t1(in a int,in b int,out c varchar(30))
begin
declare d int;
set d = a+1;
case d
when 1 then insert into student values(null,‘dx‘,11,now());
when 2 then insert into student values(null,‘aa‘,11,now());
else insert into student values(null,‘bb‘,11,now());
end case;
select * from student;
end;
再来看两个循环,一个是while do循环,一个是loop循环:
/*使用while do循环*/
create procedure t1()
begin
declare i int DEFAULT 0;
while i<5 DO
insert into student(name) values(i);
set i=i+1;
end while;
select * from student;
end;
/*使用loop循环*/
drop procedure if exists t1;
create procedure t1()
begin
declare i int DEFAULT 0;
loop_label:LOOP
if i = 3 THEN
set i = i + 1;
ITERATE loop_label; -- iterate相当于java循环里的continue
end if;
insert into student values(null,i,i,now());
set i = i + 1;
if i >= 5 THEN
leave loop_label;
end if;
end loop;
select * from student;
end;
还有比较常用的模糊查询:
/*模糊查询*/
drop procedure if exists t1;
create procedure t1(in a varchar(30),out c varchar(30))
begin
declare d int;
select * from student where name like concat(‘%‘,a,‘%‘);
end;
这个例子中要注意的是使用了concat拼接字符串函数。
版权声明:本文为博主原创文章,未经博主允许不得转载。
mysql存储过程开荒
标签:存储过程