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

来源:互联网 发布:链轮厚度计算软件 编辑:程序博客网 时间:2024/06/12 00:26

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

use shop;delimiter $create procedure addNum(in a int, in b int)begindeclare 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)begindeclare 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)begindeclare 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;
1 0