温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql基础四 存储过程

发布时间:2020-06-01 19:38:41 来源:网络 阅读:354 作者:lzf05303774 栏目:MySQL数据库

一、存储过程:变量的声明和赋值。
delimiter $

create procedure p1()

begin

declare age int default 18;

    set age :=age+20;或者   set age =age+20

select age from dual;

end$

调用存储过程:call p1(); 其结果是如下:

mysql基础四 存储过程

二、存储过程:if 控制语句。
delimiter $
create procedure p2()
begin

declare age int default 18;
if age>=18 then
 select '已成年' ;
else 
 select '未成年';
end if;

end$
调用存储过程:call p2(); 其结果是如下:
mysql基础四 存储过程

三、存储过程:输入参数。
计算一个矩形的面积,并判断是胖fat? 瘦then? 还是方square?
delimiter $
create procedure p3(w int ,h int)
begin
select concat('area:',w*h);

if w > h then
  select 'fat';
elseif w < h then
  select 'then';    
else 
  select 'square';
end if;

end$

四、存储过程:while循环

求1到100的和。
delimiter $
create procedure p100()
begin
declare total int default 0;
declare num int default 0;
while num<=100 do

set total=total+num;
set num=num+1;

end while;

select total;
end$

调用存储过程:call p100(); 其结果是如下:
mysql基础四 存储过程

五、存储过程:输出参数:
求1到n的和。
delimiter $
create procedure p8(in n int ,out total int)
begin
set total=0;
declare num int default 0;
while num<=n do

set total=total+num;
set num=num+1;

end while;

end$

调用存储过程:call p8(100,@sumary); select @sumary; 其结果是如下:

mysql基础四 存储过程

mysql基础四 存储过程

六、存储过程:输入输出参数:
delimiter $
create procedure p9(inout age int)
begin
set age =age+20;
end $

调用存储过程:set @currentage=18; call p9(@currentage) ;select @currentage; 其结果是如下:

mysql基础四 存储过程

七、存储过程:case控制语句:
delimiter $
create procedure p10()
begin

declare pos int default 0;

set pos=floor(4*rand()) ;

case pos
when 1 then select 'haha';
when 2 then select 'hehe';
else select 'heihei';
end case;

end $

八、存储过程:repeat控制语句:
delimiter $
create procedure p11()
begin

declare i int default 0;
declare total int default 0;

repeat 
    set i=i+1;
    set total =total +i;
until i>=100 end repeat;
select total;

end$

九、调用存储过程 call:

call procedure_Name();

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI