mysql存储过程的学习(一)

创建一个存储过程

create procedure myprocess()

begin

end;

为了避免存储过程中分号(";")结束语句,我们使用分隔符来判断该段命令是否已经结束了。

所以我们可以以$符号来作为结束语(亦可以用其他)

delimiter $

create procedure myprocess()

begin

end $

其实就和java里面的新增加一个方法一样 只不过这里是mysql的语法

方法里面也可以传参数

这里传参数的规则有 in out inout 

先说in 

in为入参,如果为入参,此参数只能用来传,最后的值是不会改变的,就是最后调用存储过程的时候如果要获得in入参的值,不管中间做了什么操作,这个值只能是最开始传的那个值

delimiter $

create procedure myprocess( in userid int)

begin

select * from u_user where id = userid;
end $

调用过程

call myprocess(2);

out为出参

delimiter $

create procedure myprocess( out total int)

begin

select count(1) into total  from u_user ;
end $

调用过程

注意 出参的话必须要用@打头 不然输出不了在存储过程里面直接给total赋值

调用过程

CALL myprocess(@total);
select @total;

inout 既是入参也是出参,就是将传过来的参数先进行一系列的运算之后把结果再次赋值给这个参数 输出

delimiter $

create procedure myprocess( INOUT total int)

begin
select count(1) into total from u_user where id = total;

end $

调用过程因为既是入参也是出参,所以先赋值给他,最后再查询这个值

set @total =2;
CALL myprocess(@total);

select @total;

先写到这里。。

猜你喜欢

转载自www.cnblogs.com/oushiyang/p/9238000.html