时间:2021-07-01 10:21:17 帮助过:10人阅读
存储过程语法:
create procedure procedureName
begin
sql 1
end$
显示所有的存储过程 show procedure status;
调用存储过程 call procedureName();
包含if语句的存过:
create procedure procedureName()
begin
declare yy int
if
sql1
else
sql2
end if;
end$
案例:
create procedure procedureName(width int,height int)
begin
select concat(‘你的面积是‘,width*height) as area;
if width >height then
select ‘你挺胖‘;
elseif width <height then
select ‘你挺瘦‘;
else
select ‘你挺方‘;
end if;
end$
编程三步骤
顺序 选择 循环
while循环语句的存过:
求1到100的和;
delimiter $
create procedure procedurename ()
begin
declare total int default 0;
declare num2 int default 0;
while num2<=100 do
set total:=total+num2;
set num2:=num2+1;
end while;
select total;
end$
--in传入参数
create procedure name (in num int)
begin
declare total int default 0;
declare num2 int default 0;
while num2<=num do
set total:=total+num2;
set num2:=num2+1;
end while;
select total;
end$
--in/out
delimiter$
create procedure p8 (in num int,out total int )
begin
declare num2 int default 0;
set total :=0;
while num2<=num do
set total:=total+num2;
set num2:=num2+1;
end while;
select total;
end$
调用方法:
call p8(100,@tt)
select @tt
--inout
delimiter$
create procedure p9(inout age int)
begin
set age :=age+20;
end$
调用方法:
set @currentage=18
call p9(@currentage)
select @currentage
--case
delimiter$
create procedure p10()
begin
declare pos int default 0;
set pos:=floor(4*rand());
case pos
when 1 then select ‘happy‘;
when 2 then select ‘cry‘;
when 3 then select ‘angry‘;
else select ‘none‘;
end case;
end$
--repeat循环
delimiter$
create procedure p11()
begin
declare total int default 0;
declare i int default 0;
repeat
set i:=i+1;
set total :=total+i;
until i>=100 end repeat;
select total;
end$
[MySQL]存储过程
标签:procedure concat case cat repeat else mys bsp color