2020-02-03 Mysql存储过程

一、什么是存储过程

"过程"(procedure):封装了若干条语句,调用时,这些封装体执行。
“函数”(function):是一个有返回值的过程,“过程”是没有返回值的函数。
吧这些过程存在数据库就变成了 存储过程。

三、相关语句

1.创建语句

create procedure procedureName()
begin
sql ###
end

2.查看语句

show procedure status;

3.调用存储过程

call procedureName();

4.运算

#deaclare 声明变量; set := 赋值变量 ;变量可以直接在sql中引用
create procedure p3()
begin 
 declare age int default 18;
 set age := age+20;
 select concat('20年后的年龄是:',$age);
end;

call p3();
#输出: 20年后的年龄是:38

5.if/else 控制结构
if conditon then
statement else end;

create procedure p4()
begin 
 declare age int default 90;
 if age >70 then
   select '古稀' from dual;
else
   select '风华正茂' from dual;
end if; 
end;

## 如何输入参数
create procedure p5(width int,height int)
begin 

 if width> height then
   select '宽的' from dual;
elseif width <height then
   select '长的' from dual;
else
   select '正好' from dual;
end if; 
end;

#调用
call p5(5,5);
print: 正好
call p5(4,3);
print: 宽的


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 夜莺2517阅读 127,759评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 6,954评论 1 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,603评论 28 53
  • 兔子虽然是枚小硕 但学校的硕士四人寝不够 就被分到了博士楼里 两人一间 在学校的最西边 靠山 兔子的室友身体不好 ...
    待业的兔子阅读 2,643评论 2 9