存储过程中的流程控制
在存储过程中支持流程控制语句用于实现逻辑的控制
一、分支语句
语法:if-then-else
1.单分支语句
语法
if conditions then
——SQL
end if;
if conditions then ——SQL end if;
——如果参数a的值为1,则添加一条班级信息
案例
创建一个储存过程,如果参数a的值为1,则添加一条班级信息
代码实现
创建存储过程
#创建一个储存过程 create procedure proc_test7(in a int) begin #单分支 if语句 if a=1 then insert into classes(name,class_remark) values('Java2204','test'); end if; end; #———如果参数a的值为1,则添加一条班级信息
调用存储过程
#调用存储过程 call proc_test7 (1); call proc_test7 (2);
运行结果
创建存储过程
调用存储过程
2.双分支语句
双分支:如果条件成立执行SQL1,否则执行SQL2
语法
if conditions then
——SQL1
else
——SQL2
end if;
if conditions then ——SQL1 else ——SQL2 end if;
案例
如果参数为1,创建学生信息,如果参数不为1,创建班级信息
代码实现
创建存储过程
#创建存储过程 create procedure proc_test8(in a int) begin if a=1 then insert into classes(name,class_remark) values('Java2208','test'); else insert into students(stu_num,name ,stu_gender,stu_age,cid) values('20220110','小虎','女',19,1); end if; end;
调用存储过程
#调用储存过程 call proc_test8 (1); call proc_test8 (3);
运行结果
创建存储过程
调用储存过程
3.switch case语句
语法
create procedure 储存过程名(参数)
begin
case a
when 1 then
执行的SQL语句1;
when 2 then
执行的SQL语句2;
else
执行的SQL语句3;
end case;
end;
create procedure 储存过程名(参数) begin case a when 1 then 执行的SQL语句1; when 2 then 执行的SQL语句2; else 执行的SQL语句3; end case; end;
案例
case 多分支语句
代码实现
创建储存过程
create procedure proc_test9(in num int) begin case num when 1 then #如果a的值为1,执行以下操作 insert into classes(name,class_remark) values('Java2208','test'); when 2 then #如果a的值为2,执行以下操作 insert into students(stu_num,name ,stu_gender,stu_age,cid) values('20220111','小刚','男',22,2); else #如果a的值不为1也不为2,执行以下操作 update students set stu_age=18 where stu_num ='20220110'; #修改学生年龄 end case; end;
调用储存过程
#调用储存过程 call proc_test9 (2); call proc_test9 (3);
运行结果
创建储存过程
调用储存过程
二、循环语句
1.while循环
语法
create procedure 储存过程名(传递的参数)
begin
declare i int #局部变量
set i=0 #局部变量赋值
while 循环条件 do
SQL语句
end while; #结束循环
end; #结束储存过程
create procedure 储存过程名(传递的参数) begin declare i int #局部变量 set i=0 #局部变量赋值 while 循环条件 do SQL语句 end while; #结束循环 end; #结束储存过程
案例
代码实现
创建储存过程
#while循环 创建储存过程 create procedure proc_test10(in num int) begin declare i int; set i=0; while i<num do insert into classes (name,class_remark) values(concat('Java',i),'......'); #concat()拼接字符串函数 set i=i+1; end while; end;
调用储存过程
#调用储存过程 call proc_test10 (4);
运行结果
创建储存过程
调用储存过程
执行结果
编号自动增加
2.repeat循环
案例
代码实现
创建储存过程
#repeat循环 #创建储存过程 create procedure proc_test11(in num int) begin declare i int; set i=1; repeat insert into classes (name,class_remark) values(concat('C++',i),'......'); #concat()拼接字符串函数 set i=i+1; #循环结束条件 类似于do while语句 until i>num end repeat; end;
调用储存过程
#调用储存过程 call proc_test11 (4);
运行结果
创建储存过程
调用储存过程
执行结果
3.loop循环
语法
create procedure 储存过程名(参数)
begin
declare i int; #定义局部变量
set i=0; #赋值局部变量
myloop:loop #给loop循环起名
执行的SQL语句;
set i=i+1 #迭代语句
if i=num then #循环结束条件
leave myloop;
end if; #结束判断
end loop; #结束循环
end;
create procedure 储存过程名(参数) begin declare i int; #定义局部变量 set i=0; #赋值局部变量 myloop:loop #给loop循环起名 执行的SQL语句; set i=i+1 #迭代语句 if i=num then #循环结束条件 leave myloop; end if; #结束判断 end loop; #结束循环 end;
案例
创建储存过程
loop == 循环+判断
代码实现
创建储存过程
#创建储存过程 # loop == 循环+判断 create procedure proc_test12(in num int) begin declare i int; set i=0; myloop:loop insert into classes (name,class_remark) values(concat('Python',i),'......'); set i=i+1; if i=num then leave myloop; end if; end loop; end;
调用储存过程
#调用储存过程 call proc_test12(4);
运行结果
创建储存过程
调用储存过程
执行结果