MySQL里用存储过程实现加减乘除

  MySQL从5.0版本开始支持存储过程procedure,下面介绍在MySQL5.1中使用存储过程实现整数的加减乘除。
  创建存储过程时,需要依赖某个数据库,可以任意指定,比如数据库shop。
  1.用存储过程实现整数相加  

use shop;

delimiter $
create procedure addNum(in a int, in b int)
begin
declare res int default 0;
if a is null then 
    set a=0;
end if;
if b is null then
    set b=0;
end if;
/* a+b */
set res=a+b;
/* output the result */
select res;
end;
$
delimiter ;

  2.用存储过程实现整数相减 

use shop;

delimiter $
create procedure subNum(in a int, in b int)
begin
declare res int default 0;
if a is null then 
    set a=0;
end if;
if b is null then
    set b=0;
end if;
/* a-b */
set res=a-b;
/* output the result */
select res;
end;
$
delimiter ;

  3.用存储过程实现整数相乘

use shop;

delimiter $
create procedure multiNum(in a int, in b int)
begin
declare res int default 0;
if a is null then 
    set a=0;
end if;
if b is null then
    set b=0;
end if;
/* a*b */
set res=a*b;
/* output the result */
select res;
end;
$
delimiter ;

  4.用存储过程实现整数相除

use shop;

delimiter $
create procedure divNum(in a int, in b int)
begin
/* res is float */
declare res float default 0;
if a is null then 
    set a=0;
end if;
if b is null then
    set b=0;
end if;
/* a/b */
if b=0 then
    set res=null;
else
    set res=a/b;
end if;
/* output the result */
select res; 
end;
$
delimiter ;

  整数3/5=0.6,效果如下:

这里写图片描述
图(1)整数相除的效果

  调用存储过程,对两个整数进行加减乘除运算的效果,分别如图(2)所示:
这里写图片描述
图(2) 实现整数的四则运算

  5.查看当前数据库里所有的存储过程
  show procedure status;
  
  6.查看某个存储过程
  show create procedure your_proce_Name 

/* 比如:*/
show create procedure addNum;

  7.删除当前数据库的某个存储过程
  drop procedure your_proce_Name;

/* 比如 */
drop procedure addNum;
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值