mysql 带参数的存储过程

参数类型包括三种:“in”,"out","‘inout’

三者的区别是:“in”作为传入参数,“out”传出参数,"inout"需要传入参数之后经过计算再次传出,默认为“in”.


一、带"in"参数的存储过程(计算一个累加函数)

delimiter//

create PROCEDURE test1(in n int)
BEGIN
declare i INT default 0;
declare a int default 0;
while i <=n do
set a=a+i;
set i=i+1;
end while;
select a;#也可以放在call下面,变成select @a,结果为@a=45
end//
delimiter;

call test1(9)

结果

a
45


二、带"out"参数的存储过程

delimiter//

create PROCEDURE test2(in n int,out a int)
BEGIN
declare i INT default 0;
set a=0;#out参数如果没有set默认值会显示无结果
while i <=n do
set a=a+i;
set i=i+1;
end while;
end//
delimiter;

call test2(9,@a);
select @a;

@a
45

在存储过程中,该数的初始值为null,无论在外界是否有定值,所以只能在内部再set一次

drop procedure if exists test2;
delimiter//

create PROCEDURE test2(in n int,out a int)
BEGIN
declare i INT default 0;
while i <=n do
set a=a+i;
set i=i+1;
end while;
end//
delimiter;

set @a=0;
call test2(9,@a);
select @a;

@a
NULL



三、带"inout"参数的存储过程

delimiter //
CREATE PROCEDURE test3(inOUT age INT)
BEGIN
set age=age+18;
select age;
END //
SET @age = 10;
CALL test3(@age);

age
28


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值