mysql存儲過程小點

drop procedure if exists mypro2;
create procedure mypro2(in score int)
BEGIN
if score >= 60 then
select 'pass';
ELSE
select 'no';
end if;
end;
call mypro2(150);
-----------------------------------------------------
drop procedure if exists mypro3;
create procedure mypro3(out score int)
begin
set score = 100;
end;
call mypro3(@aa);
select @aa;
------------------------------------------------------
drop procedure if exists mypro4;
create procedure mypro4(inout score int)
begin
select score;
end;
set @aa=50;    
call mypro4(@aa);  --不能直接用數字
------------------------------------------------------
drop  procedure if exists mypro5;
create procedure mypro5(in a int, in b int) -------- 多參數
BEGIN
declare c int default 0;  -- 必須要聲明這個變量
set c = a + b;
select c;
end;
call mypro5(20, 30);
------------------ case when -------------------------
drop procedure if exists mypro6;
create procedure mypro6(in score int)
begin
case score
when 20 then select '>20';
when 30 then select '>30';
when 40 then select '>40';
when 50 then select '>50';
when 60 then select '>60';
else select 'well';  -- 這裏和其他語言不一樣
end case;
end;
call mypro6(55);
------------------- while  ---------------------------
drop procedure if exists mypro7;
create procedure mypro7()
begin
declare i int default 0;
declare j int default 0;
while i < 10 do
set j = j + i;
set i = i + 1;
end while;
select j;
end;
call mypro7();
------------------- repeat until ----------------------
drop procedure if exists mypro8;
create procedure mypro8()
begin
declare i int default 0;
declare j int default 0;
repeat
set j = j +i;
set i = i + 1;
until j >= 10     -- 條件語句沒有;
end REPEAT;       -- 邏輯結束需要;
select j;
end;
call mypro8();
------------------- loop ------------------------------
drop procedure if exists mypro9;
create procedure mypro9()
begin
declare i int default 0;
declare s int default 0;
loop_lable:loop             -- loop_lable 控制退出loop的時候需要
set s = s + 1;
set i = i + 1;
if i >= 10 then
leave loop_lable;           -- 退出loop
end if;
end loop;
select s;
end;
call mypro9();

show procedure status like '%9';   -- 查看存儲過程的狀態
show create procedure mypro9;      -- 查看存儲過程代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章