MySQL数据库中如何新建一个带参数的存储过程procedure

我们都知道,现在MySQL的版本,可以支持存储过程了。我使用的是MySQL5.5的版本。


1,创建存储过程


delimiter $

create procedure in_out_procedure(in procedure_id int,out count_number int)
begin
select count(*) into count_number from temp_url where id=procedure_id;
end $

delimiter ;

注意:delimiter $ 改变分隔符,MySQL数据库中,使用delimiter 关键字来改变分隔符,当我们在创建触发器或者存储过程的时候,经常会用到。


2,执行存储过程:


call in_out_procedure(1,count_number);
MySQL如何调用带参数的存储过程?
call in_out_procedure(1,@param1);

select @param1;

注意,带有输出out的存储过程,我们在使用时,需要先获得输出结果,这里我们使用参数param1,相当于是接收返回结果,然后就可以使用这个返回结果了。


3,再来看下面的例子:


delimiter $
create procedure out_procedure(out count_number int)
begin
select count(*) into count_number from temp_url where id=1;
end $
delimiter ;

执行存储过程:
call out_procedure(@param1);
select @param1;

4,如何查询数据库中所有的存储过程?


第一种方式:show procedure status;


第二种方式:select name from mysql.proc where db="test" and type="procedure";


通过第一种方式,可以查询当前数据库中所有的存储过程的详细信息,但是不包含创建存储过程的语句;
通过第二种方式,可以查询制定数据库中所有的存储过程的名称;
然后根据存储过程的名称,我们就可以查询这个存储过程的创建语句了,用下面的语法:
show create procedure procedure_name;
例如,我们要查询名称为:in_out_procedure的存储过程,就这样写:

show create procedure in_out_procedure;



你可能感兴趣的:(存储过程,mysql,数据库,Mysql存储过程)