信息技術(shù)咨詢(xún)公司鄭州靠譜seo電話
存儲(chǔ)過(guò)程中的流程控制
在存儲(chǔ)過(guò)程中支持流程控制語(yǔ)句用于實(shí)現(xiàn)邏輯的控制
一、分支語(yǔ)句
語(yǔ)法:if-then-else
1.單分支語(yǔ)句
語(yǔ)法
if conditions then
? ? ? ? ? ? ?——SQL
end if;
if conditions then——SQLend if;
——如果參數(shù)a的值為1,則添加一條班級(jí)信息
案例
創(chuàng)建一個(gè)儲(chǔ)存過(guò)程,如果參數(shù)a的值為1,則添加一條班級(jí)信息
代碼實(shí)現(xiàn)
創(chuàng)建存儲(chǔ)過(guò)程
#創(chuàng)建一個(gè)儲(chǔ)存過(guò)程
create procedure proc_test7(in a int)
begin#單分支 if語(yǔ)句if a=1 theninsert into classes(name,class_remark) values('Java2204','test');end if;
end;
#———如果參數(shù)a的值為1,則添加一條班級(jí)信息
調(diào)用存儲(chǔ)過(guò)程
#調(diào)用存儲(chǔ)過(guò)程
call proc_test7 (1);
call proc_test7 (2);
運(yùn)行結(jié)果
創(chuàng)建存儲(chǔ)過(guò)程
調(diào)用存儲(chǔ)過(guò)程
2.雙分支語(yǔ)句
雙分支:如果條件成立執(zhí)行SQL1,否則執(zhí)行SQL2
語(yǔ)法
if conditions then?
? ? ? ? ——SQL1
else
? ? ? ? ——SQL2
end if;
if conditions then?——SQL1else——SQL2end if;
案例
如果參數(shù)為1,創(chuàng)建學(xué)生信息,如果參數(shù)不為1,創(chuàng)建班級(jí)信息
代碼實(shí)現(xiàn)
創(chuàng)建存儲(chǔ)過(guò)程
#創(chuàng)建存儲(chǔ)過(guò)程
create procedure proc_test8(in a int)
beginif a=1 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;
調(diào)用存儲(chǔ)過(guò)程
#調(diào)用儲(chǔ)存過(guò)程
call proc_test8 (1);
call proc_test8 (3);
運(yùn)行結(jié)果
創(chuàng)建存儲(chǔ)過(guò)程
調(diào)用儲(chǔ)存過(guò)程
3.switch case語(yǔ)句
語(yǔ)法
create procedure 儲(chǔ)存過(guò)程名(參數(shù))
begin
? ? ? ? case a
? ? ? ? ????????when 1 then
? ? ? ? ? ? ? ? ????????執(zhí)行的SQL語(yǔ)句1;
? ? ? ????????? when 2 then
? ? ? ? ? ? ? ? ? ? ? ? 執(zhí)行的SQL語(yǔ)句2;
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? 執(zhí)行的SQL語(yǔ)句3;
? ? ? ? end case;
end;
create procedure 儲(chǔ)存過(guò)程名(參數(shù))begincase awhen 1 then執(zhí)行的SQL語(yǔ)句1;when 2 then執(zhí)行的SQL語(yǔ)句2;else執(zhí)行的SQL語(yǔ)句3;end case;end;
案例
case 多分支語(yǔ)句
代碼實(shí)現(xiàn)
創(chuàng)建儲(chǔ)存過(guò)程
create procedure proc_test9(in num int)
begincase numwhen 1 then#如果a的值為1,執(zhí)行以下操作insert into classes(name,class_remark) values('Java2208','test');when 2 then#如果a的值為2,執(zhí)行以下操作insert into students(stu_num,name ,stu_gender,stu_age,cid)values('20220111','小剛','男',22,2);else#如果a的值不為1也不為2,執(zhí)行以下操作update students set stu_age=18 where stu_num ='20220110';#修改學(xué)生年齡end case;
end;
調(diào)用儲(chǔ)存過(guò)程
#調(diào)用儲(chǔ)存過(guò)程
call proc_test9 (2);
call proc_test9 (3);
運(yùn)行結(jié)果
創(chuàng)建儲(chǔ)存過(guò)程
調(diào)用儲(chǔ)存過(guò)程
二、循環(huán)語(yǔ)句
1.while循環(huán)
語(yǔ)法
create procedure 儲(chǔ)存過(guò)程名(傳遞的參數(shù))
begin
? ? ? ? declare i int????????#局部變量
? ? ? ? ????????set i=0? ? ? ? #局部變量賦值
? ? ? ? ? ? ? ? while 循環(huán)條件 do
? ? ? ? ? ? ? ? ? ? ? ? SQL語(yǔ)句
? ? ? ? ? ? ? ? end while;? ? ? ? #結(jié)束循環(huán)
? ? ? ? end;? ? ? ? ? ? ? ? ? ? ? ? #結(jié)束儲(chǔ)存過(guò)程
create procedure 儲(chǔ)存過(guò)程名(傳遞的參數(shù))begindeclare i int????????#局部變量set i=0? ? ? ? #局部變量賦值while 循環(huán)條件 doSQL語(yǔ)句end while;? ? ? ? #結(jié)束循環(huán)end;? ? ? ? ? ? ? ? ? ? ? ? #結(jié)束儲(chǔ)存過(guò)程
案例
代碼實(shí)現(xiàn)
創(chuàng)建儲(chǔ)存過(guò)程
#while循環(huán) 創(chuàng)建儲(chǔ)存過(guò)程
create procedure proc_test10(in num int)
begindeclare i int;set i=0;while i<num doinsert into classes (name,class_remark)values(concat('Java',i),'......');#concat()拼接字符串函數(shù)set i=i+1;end while;
end;
調(diào)用儲(chǔ)存過(guò)程
#調(diào)用儲(chǔ)存過(guò)程
call proc_test10 (4);
運(yùn)行結(jié)果
創(chuàng)建儲(chǔ)存過(guò)程
調(diào)用儲(chǔ)存過(guò)程
執(zhí)行結(jié)果
編號(hào)自動(dòng)增加
2.repeat循環(huán)
案例
代碼實(shí)現(xiàn)
創(chuàng)建儲(chǔ)存過(guò)程
#repeat循環(huán)
#創(chuàng)建儲(chǔ)存過(guò)程
create procedure proc_test11(in num int)
begindeclare i int;set i=1;repeatinsert into classes (name,class_remark)values(concat('C++',i),'......');#concat()拼接字符串函數(shù)set i=i+1;#循環(huán)結(jié)束條件 類(lèi)似于do while語(yǔ)句until i>numend repeat;
end;
調(diào)用儲(chǔ)存過(guò)程
#調(diào)用儲(chǔ)存過(guò)程
call proc_test11 (4);
運(yùn)行結(jié)果
創(chuàng)建儲(chǔ)存過(guò)程
調(diào)用儲(chǔ)存過(guò)程
執(zhí)行結(jié)果
3.loop循環(huán)
語(yǔ)法
create procedure 儲(chǔ)存過(guò)程名(參數(shù))
begin
? ? ? ? declare i int;????????#定義局部變量
? ? ? ? set i=0;? ? ? ? ? ? ? ? #賦值局部變量
? ? ? ? myloop:loop? ? ? ? #給loop循環(huán)起名
? ? ? ? ? ? ? ? 執(zhí)行的SQL語(yǔ)句;
? ? ? ? ? ? ? ? set i=i+1? ? ? ? #迭代語(yǔ)句
? ? ? ? ? ? ? ?if i=num then? #循環(huán)結(jié)束條件
? ? ? ? ? ? ? ? ? ? ? ? leave myloop;
? ? ? ? ? ? ? ? end if;? ? ? ? ? ? ?#結(jié)束判斷
????????end loop;? ? ? ? ? ? ? ? #結(jié)束循環(huán)
end;
create procedure 儲(chǔ)存過(guò)程名(參數(shù))begindeclare i int;????????#定義局部變量set i=0;? ? ? ? ? ? ? ? #賦值局部變量myloop:loop? ? ? ? #給loop循環(huán)起名執(zhí)行的SQL語(yǔ)句;set i=i+1? ? ? ? #迭代語(yǔ)句if i=num then? #循環(huán)結(jié)束條件leave myloop;end if;? ? ? ? ? ? ?#結(jié)束判斷end loop;? ? ? ? ? ? ? ? #結(jié)束循環(huán)end;
案例
創(chuàng)建儲(chǔ)存過(guò)程
loop == 循環(huán)+判斷
代碼實(shí)現(xiàn)
創(chuàng)建儲(chǔ)存過(guò)程
#創(chuàng)建儲(chǔ)存過(guò)程
# loop == 循環(huán)+判斷
create procedure proc_test12(in num int)
begindeclare i int;set i=0;myloop:loopinsert into classes (name,class_remark)values(concat('Python',i),'......');set i=i+1;if i=num thenleave myloop;end if;end loop;
end;
調(diào)用儲(chǔ)存過(guò)程
#調(diào)用儲(chǔ)存過(guò)程
call proc_test12(4);
運(yùn)行結(jié)果
創(chuàng)建儲(chǔ)存過(guò)程
調(diào)用儲(chǔ)存過(guò)程
?