定义语法:
create procedure 存储名(参数)
begin
存储体
end
1)无参模式:
use test1
create procedure select_all_data()
begin
select * from emploee;
end
#调用存储过程
call select_all_data()
2)out参数模式:
create procedure select_max_salary(out max_sal decimal)
begin
select max(salary) into max_sal from emploee;
end
#调用存储过程
call select_max_salary(@max_sal) #注存储过程没有返回值,通过select查询结果
select @max_sal
3)in参数模式:
create procedure select_some_message(in nn varchar(20))
begin
select * from emploee where name=nn ;
end
#调用存储过程方式一:
set @nn='tom'
call select_some_message (@nn)
#调用存储过程方式二:
call select_some_message ('tom')
4)in和out参数模式:
create procedure select_some_salary(in nn varchar(20),out sal decimal(10,2))
begin
select salary into sal from emploee where name=nn;
end
#调用存储过程:
call select_some_salary('jack',@sal)
select @sal
5)inout参数模式:
create procedure select_leader_message(inout ne varchar(12))
begin
select name into ne from emploee where id=(
select manger_id from emploee where name=ne);
end
#调用存储过程:
set @ne='jack'
call select_leader_message(@ne)
select @ne
6)加分隔符模式:(部分版本不支持delimiter重新定义分隔符)
delimiter $
create procedure select_all_data()
begin
select * from emploee;
end $
delimiter ;
#调用存储过程
call select_all_data()
7)查看存储过程信息
show create procedure select_leader_message
8)删除存储过程
drop procedure select_all_data
注:
1)存储过程优点是执行速度快,可以复用,缺点是可移植性差,调试困难,没有好的集成工具支持。