公司自己做网站备案,wordpress链接出错,优化优化,一个新产品的营销方案存储过程中的流程控制
在存储过程中支持流程控制语句用于实现逻辑的控制
一、分支语句
语法#xff1a;if-then-else 1.单分支语句
语法
if conditions then ——SQL
end if;
if conditions then——SQLend if;
——如果参数a的值为1#xff0c;则添加一条班级信息
…存储过程中的流程控制
在存储过程中支持流程控制语句用于实现逻辑的控制
一、分支语句
语法if-then-else 1.单分支语句
语法
if conditions then ——SQL
end if;
if conditions then——SQLend if;
——如果参数a的值为1则添加一条班级信息
案例
创建一个储存过程如果参数a的值为1则添加一条班级信息
代码实现
创建存储过程
#创建一个储存过程
create procedure proc_test7(in a int)
begin#单分支 if语句if a1 theninsert 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 ——SQL1else——SQL2end if;
案例
如果参数为1创建学生信息如果参数不为1创建班级信息
代码实现
创建存储过程
#创建存储过程
create procedure proc_test8(in a int)
beginif a1 theninsert into classes(name,class_remark) values(Java2208,test);elseinsert 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 储存过程名(参数)begincase awhen 1 then执行的SQL语句1when 2 then执行的SQL语句2else执行的SQL语句3end case;end;
案例
case 多分支语句
代码实现
创建储存过程
create procedure proc_test9(in num int)
begincase numwhen 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_age18 where stu_num 20220110;#修改学生年龄end case;
end;调用储存过程
#调用储存过程
call proc_test9 (2);
call proc_test9 (3);运行结果
创建储存过程 调用储存过程 二、循环语句
1.while循环 语法
create procedure 储存过程名传递的参数
begin declare i int #局部变量 set i0 #局部变量赋值 while 循环条件 do SQL语句 end while; #结束循环 end; #结束储存过程
create procedure 储存过程名传递的参数begindeclare i int #局部变量set i0 #局部变量赋值while 循环条件 doSQL语句end while; #结束循环end; #结束储存过程
案例
代码实现
创建储存过程
#while循环 创建储存过程
create procedure proc_test10(in num int)
begindeclare i int;set i0;while inum doinsert into classes (name,class_remark)values(concat(Java,i),......);#concat()拼接字符串函数set ii1;end while;
end;调用储存过程
#调用储存过程
call proc_test10 (4);
运行结果
创建储存过程 调用储存过程 执行结果 编号自动增加
2.repeat循环
案例
代码实现
创建储存过程
#repeat循环
#创建储存过程
create procedure proc_test11(in num int)
begindeclare i int;set i1;repeatinsert into classes (name,class_remark)values(concat(C,i),......);#concat()拼接字符串函数set ii1;#循环结束条件 类似于do while语句until inumend repeat;
end;调用储存过程
#调用储存过程
call proc_test11 (4);
运行结果
创建储存过程 调用储存过程 执行结果 3.loop循环
语法
create procedure 储存过程名参数
begin declare i int; #定义局部变量 set i0; #赋值局部变量 myloop:loop #给loop循环起名 执行的SQL语句; set ii1 #迭代语句 if inum then #循环结束条件 leave myloop; end if; #结束判断 end loop; #结束循环
end;
create procedure 储存过程名参数begindeclare i int; #定义局部变量set i0; #赋值局部变量myloop:loop #给loop循环起名执行的SQL语句;set ii1 #迭代语句if inum then #循环结束条件leave myloop;end if; #结束判断end loop; #结束循环end;
案例
创建储存过程
loop 循环判断
代码实现
创建储存过程
#创建储存过程
# loop 循环判断
create procedure proc_test12(in num int)
begindeclare i int;set i0;myloop:loopinsert into classes (name,class_remark)values(concat(Python,i),......);set ii1;if inum thenleave myloop;end if;end loop;
end;
调用储存过程
#调用储存过程
call proc_test12(4);
运行结果
创建储存过程 调用储存过程 执行结果