mysql的存储过程的参数算法运算_万金油_新浪博客

参数类型
• MySQL 存储过程,共有三种参数类型 IN,OUT,INOUT
Create procedure 名称 (类型 参数名 数据类型 ,类型 参数名 数据类型)
in 输入参数      作用是给存储过程传值,必须在调用存储过程时赋值,在存储过程中该参数的值不允许修改;默认类型是 in

out 输出参数       该值可在存储过程内部被改变,并可返回

inout 输入 / 输出参数             调用时指定,并且可被改变和返回

注意:此三中类型的变量在存储过程中调用时不需要加 @ 符号 !!!

实验:
mysql> delimiter //
mysql> create procedure p4(in shellname char(20))       //默认就是in参数
    -> begin
    -> select count(name) from user where shell=shellname;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call p4("/bin/bash");
+-------------+
| count(name) |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call p4("/sbin/nologin");
+-------------+
| count(name) |
+-------------+
|          35 |
+-------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call p4();
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE db9.p4; expected 1, got

给输入参数给与变量:
mysql> set @x = "/sbin/nologin";   //只有用户变量需要加@
   
Query OK, 0 rows affected (0.00 sec)

mysql> call p4(@x);
+-------------+
| count(name) |
+-------------+
|          35 |
+-------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

out 输出参数       该值可在存储过程内部被改变,并可返回
实验:

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter //
mysql> create procedure p7(out number int(2))
    -> begin
    -> set number = 9;         //定义变量不能加@符合,加了就相当于用户定义参数
    -> select number;
    -> select count(id) into number from user;
    -> select number;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call p7();
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE db9.p7; expected 1, got 0

mysql> select @w;      //给于用户定义变量, 相当于占位
+------+
| @w   |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

mysql> call p7(@w);
+--------+
| number |
+--------+
|      9 |
+--------+
1 row in set (0.00 sec)

+--------+
| number |
+--------+
|     41 |
+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call p7(8);
ERROR 1414 (42000): OUT or INOUT argument 1 for routine db9.p7 is not a variable or NEW pseudo-variable in BEFORE trigger
//out参数不能赋值,只能输出;

算法运算:

算数运算
• 算数运算符号

例子
+ 加法运算 SET @var1=2+2;   4
- 减法运算 SET @var2=3-2;    1
* 乘法运算 SET @var3=3*2 ;   
/ 除法运算 SET @var4=10/3; 3.333333333
DIV 整除运算 SET @var5=10  DIV 3; 3
%取模 SET @var6=10%3 ;     1

mysql> set @z=1+2;select @z;
mysql> set @x=1; set @y=2;set @z=@x*@y; select @z;
mysql> set @x=1; set @y=2;set @z=@x-@y; select @z;
mysql> set @x=1; set @y=2;set @z=@x/@y; select @z;

运算实例:
mysql> delimiter //
mysql> create procedure say(in bash char(10),in nologin char(15),out x int(2),out y int(2))
    -> begin
    -> declare z int(2);
    -> set z =0;
    -> select count(shell) into x from user where shell=bash;
    -> select count(shell) into y from user where shell=nologin;
    -> set z = x + y;
    -> select z;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call say("/bin/bash","/sbin/nologin",@x,@y);
+------+
| z    |
+------+
|   37 |
+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维螺丝钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值