您的位置:首页 > 数据库 > MySQL

【MySql】5.存储过程的使用

2014-05-06 10:22 453 查看
存储过程简单来说,就是为以后的使用而保存的一条或多条MySQL语句的集合。可将其视为批文件,虽然它们的作用不仅限于批处理。使用存储过程需要MySQL5及以后的版本支持。

一、为什么要使用存储过程

通过把处理封闭在容易使用的单元中,简化复杂的操作;将一系列处理步骤放到同一存储过程中,保证了数据的完整性和操作的安全性;简化对变更的管理;提高性能。
使用存储过程比使用单独的SQL语句要快;
存在一些只能用在单个请求中的MySQL元素和特性,存储过程可以使用它们来编写功能更强更灵活的代码;

二、基本操作

1、创建存储过程

CREATE PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
create procedure sp_test()
begin select name,classes_name from student222;end

2、执行存储过程

CALL sp_name;call sp_test;

3、删除存储过程

DROP PROCEDURE [ IF EXISTS ] sp_name;drop procedure if exists sp_test;

4、查看存储过程创建信息

SHOW CREATE PROCEDURE sp_name;
show create procedure sp_test;






5、查看存储过程状态

show procedure status like 'sp_test';


6、使用存储过程参数

(1)IN参数:只用来向过程传递信息,为默认值,在MySQL存储过程内部可能会修改此参数,但in类型参数的修改对调用者(caller)来说是不可见的。
create procedure pr_param_in(in id int)
begin
if (id is not null) then
set id=id+1;
end if;
select id as id_inner;
end;

set @id=10;
call pr_param_in(@id);




select @id as id_out;



可以看到用户变量@id传入值为10,执行存储过程后,在过程内部值为:11(id_inner),但外部变量值依旧为:10(id_out)
===================================================================================
(2)OUT参数:只用来从过程传回信息,传值给调用者,在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。
create procedure pr_param_out(out id int)
begin
select id as id_inner_1;
if (id is not null) then
set id=id+1;
select id as id_inner_2;
else
select 1 into id; -- 使用select...into...传回值给out参数
end if;
select id as id_inner_3;
end;

set @id=10;
call pr_param_out(@id);






select @id as id_out;




可以看出,虽然我们设置了用户定义变量@id为10,传递@id给存储过程后,在存储过程内部,id的初始值总是 null(id_inner_1)。最后id值(id_out=1)传回给调用者。
===================================================================================
(3)INOUT参数可以向过程传递信息,也可以从存储过程内部传值给调用者。
create procedure pr_param_inout(inout id int)
begin
select id as id_inner_1;
if (id is not null) then
set id=id+1;
select id as id_inner_2;
else
select 1 into id;
end if;
select id as id_inner_3;
end;

set @id=10;
call pr_param_inout(@id);









select @id as id_out;




从结果可以看出:我们把 @id(10)传给存储过程后,存储过程最后又把计算结果值11(id_inner_3)
传回给调用者。
===================================================================================
通过以上例子:
1) 如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;
2) 如果仅仅从MySQL存储过程返回值,那就用out类型参数;
3) 如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。
===================================================================================
【参数名和列名不要重名,否则会出错;BEGIN..End间声明的变量名不能与关键字重名;mysql中不能采用select left1=Lft from table where...的方式获取值!只能用set为变量赋值】----这个规则也适用于函数
create procedure CountLayer(IN nodeid int,OUT x1 int)
begin
declare left1 int;
declare right1 int;
set left1=(select Lft from treelevel where Node_id = nodeid);
set right1=(select Rgt from treelevel where Node_id = nodeid);
select count(*) into x1 from treelevel where Lft<=left1 and Rgt>=right1;
end

call CountLayer(1,@xx);
select @xx;
===================================================================================
create procedure sp_type_cnt(IN in_type varchar(20),OUT out_cnt int)
BEGIN
select count(*) from classes222 where name =in_type into out_cnt;
END

call sp_type_cnt('AA班',@cnt);

select @cnt;


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: