存储过程传参:存储过程的括号里,可以声明参数。 语法是 create procedure p([in/out/inout] 参数名 参数类型 ..)in :给参数传入值,定义的参数就得到了值out:模式定义的参数只能在过程体内部赋值,表示该参数可以将某个值传递回调用他的过程(在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值)inout:调用者还可以通过 inout 参数传递值给存储过程,也可以从存储过程内部传值给调用者
如果仅仅想把数据传给 mysql 存储过程,那就使用“in” 类型参数;如果仅仅从 mysql 存储过程返回值,那就使用“out” 类型参数;如果需要把数据传给 mysql 存储过程,还要经过一些计算后再传回给我们,此时,要使用“inout” 类型参数。 mysql 存储过程参数如果不显式指定in、out、inout,则默认为in。
实例一:存储过程传参 indelimiter $$create procedure p1(in num int)begin declare i int default 0; declare total int default 0; while i<=num do set total := i + total; set i := i+1; end while; select total;end$$
实例二:存储过程传参 outcreate procedure p2(out num int)begin select num as num_1; if (num is not null) then set num = num + 1; select num as num_2; else select 1 into num; end if; select num as num_3;end$$set @num = 10$$call p2(@num)$$select @num as num_out$$
实例三:存储过程传参 inoutcreate procedure p3(inout age int)begin set age := age + 20;end$$set @currage =18$$call p3(@currage)$$select @currage$$