创建存储过程:
create procedure 存储名字(参数列表)
begin
存储过程体;
end
参数列表:in,out,inout
in 字段名 字段类型
out 字段名 字段类型
inout 字段名 字段类型
delimiter 分隔符
调用存储:
call 存储名(实参列表);
例子:in
创建
delimiter $
create procedure mmp()
begin
insert into 表(id,name)
values(1,'2'),(2,'2'),(3,'3');
update 表 set name='33' where id=2;
delete from 表 where id=1;
end $
调用
call mmp()$
查看结果
selelct * from 表$
"使用用户名密码登陆,成功或者识别"
delimiter $
create procedure mmp1(in username varchar(11),in password int(11))
begin
declare 局部变量 int(11) default 0;
select count(*) into 局部变量
from 表
where 表.username = username
and 表.password = password;
select if(局部变量>0,'成功','失败');
end $
call mmp1(波多野结衣,88888888)$
select * from 表$
例子:out
"输入一个name,返回他的密码和爱好"
delimiter $
create procedure mmp2(in username varchar(11),out password int(11),out likehao varchar(11))
begin
select 表1.password,表2.likehao into password,likehao
from 表1
join 表2 on 表1.id=表2.id
where 表1.username = username;
end $
set @pword,@lhao$
call mmp2('波多野结衣',@pword,@lhao)$
select @pword,@lhao$
例子:inout
"举个不一样的例子,输入一个数,返回他的2倍"
delimiter $
create procedure mmp3(inout a int ,inout b int)
begin
set a=a*2;
set b=b*2;
end $
set @a=100,@b=200$
call mmp3(@a,@b)$
删除存储过程:drop procedure
drop procedure mmp3;
查看存储过程的信息:show create procedure
show create procedure mmp1;