美文网首页
2020-02-03 Mysql存储过程

2020-02-03 Mysql存储过程

作者: 黄哈哈哈哈哈 | 来源:发表于2021-03-26 16:04 被阅读0次

一、什么是存储过程

"过程"(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: 宽的


相关文章

网友评论

      本文标题:2020-02-03 Mysql存储过程

      本文链接:https://www.haomeiwen.com/subject/iclhxhtx.html