`

MySQL 存储过程参数用法 in, out, inout(转)

    博客分类:
  • sql
阅读更多

MySQL 存储过程参数有三种类型:in、out、inout。它们各有什么作用和特点呢?

一、MySQL 存储过程参数(in)

MySQL 存储过程 “in” 参数:跟 C 语言的函数参数的值传递类似, MySQL 存储过程内部可能会修改此参数,但对 in 类型参数的修改,对调用者(caller)来说是不可见的(not visible)。

										
drop procedure if exists pr_param_in;

create procedure pr_param_in
(
   in id int -- in 类型的 MySQL 存储过程参数
)
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;
										
mysql> call pr_param_in(@id);
+----------+
| id_inner |
+----------+
|       11 |
+----------+

mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 10     |
+--------+

可以看到:用户变量 @id 传入值为 10,执行存储过程后,在过程内部值为:11(id_inner),但外部变量值依旧为:10(id_out)。

二、MySQL 存储过程参数(out)

MySQL 存储过程 “out” 参数:从存储过程内部传值给调用者。在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。

										
drop procedure if exists pr_param_out;

create procedure pr_param_out
(
   out id int
)
begin
   select id as id_inner_1;  -- id 初始值为 null

   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_out(@id);

select @id as id_out;
										
mysql> set @id = 10;
mysql>
mysql> call pr_param_out(@id);
+------------+
| id_inner_1 |
+------------+
|       NULL |
+------------+

+------------+
| id_inner_3 |
+------------+
|          1 |
+------------+

mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 1      |
+--------+

可以看出,虽然我们设置了用户定义变量 @id 为 10,传递 @id 给存储过程后,在存储过程内部,id 的初始值总是 null(id_inner_1)。最后 id 值(id_out = 1)传回给调用者。

三、MySQL 存储过程参数(inout)

MySQL 存储过程 inout 参数跟 out 类似,都可以从存储过程内部传值给调用者。不同的是:调用者还可以通过 inout 参数传递值给存储过程。

										
drop procedure if exists pr_param_inout;

create procedure pr_param_inout
(
   inout id int
)
begin
   select id as id_inner_1;  -- id 值为调用者传进来的值

   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;
										
mysql> set @id = 10;

mysql>
mysql> call pr_param_inout(@id);
+------------+
| id_inner_1 |
+------------+
|         10 |
+------------+

+------------+
| id_inner_2 |
+------------+
|         11 |
+------------+

+------------+
| id_inner_3 |
+------------+
|         11 |
+------------+
mysql>
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 11     |
+--------+

从结果可以看出:我们把 @id(10),传给存储过程后,存储过程最后又把计算结果值 11(id_inner_3)传回给调用者。 MySQL 存储过程 inout 参数的行为跟 C 语言函数中的引用传值类似。

通过以上例子:如果仅仅想把数据传给 MySQL 存储过程,那就使用“in” 类型参数;如果仅仅从 MySQL 存储过程返回值,那就使用“out” 类型参数;如果需要把数据传给 MySQL 存储过程,还要经过一些计算后再传回给我们,此时,要使用“inout” 类型参数。

分享到:
评论

相关推荐

    mysql存储过程之返回多个值的方法示例

    要开发返回多个值的存储过程,需要使用带有INOUT或OUT参数的存储过程。咱们先来看一个orders表它的结构: mysql> desc orders; +----------------+-------------+------+-----+---------+-------+ | Field | Type |...

    MySQL数据库:存储过程的创建1.pptx

    3)掌握 —— 存储过程的创建方法; 存储过程优点 使用存储过程的优点有: (1)存储过程在服务器端运行,执行速度快。 (2)存储过程执行一次后,其执行规划就驻留在高速缓冲存储器,在以后的操作中,只需从高速...

    mysql 存储过程输入输出参数示例

    drop PROCEDURE if exists my_procedure; create PROCEDURE my_procedure(in my_id int,out my_name... 您可能感兴趣的文章:Mysql存储过程循环内嵌套使用游标示例代码MySQL 存储过程中执行动态SQL语句的方法Mysql存储过

    MySQL数据库:存储过程嵌套.pptx

    创建另外一个存储过程sell_update,在其中调用第一个存储过程,如果给定参数为0,则修改由第一个存储过程插入记录的是否发货字段为'已发货',如果给定参数为1则删除第一个存储过程插入的记录,并将操作结果输出。...

    PHP调用MySQL存储过程并返回值的方法

    如果存储过程有 IN/INOUT参数,声明一个变量,输入参数给存储过程,该变量是一对,一个php变量,也可以不必,只是没有php变量时,没有办法进行动态输入,一个Mysql变量. b。如果存储过程有OUT变量,声明一个Mysql变量,mysql...

    mysql数据库my.cnf配置文件

    # MyISAM设置恢复表之时使用的缓冲区的尺寸,当在REPAIR TABLE或用CREATE INDEX创建索引或ALTER TABLE过程中排序 MyISAM索引分配的缓冲区 myisam_max_sort_file_size = 10G # 如果临时文件会变得超过索引,不要使用...

    第六章:MySQL高级进阶-存储过程及基本使用

    存储过程(Stored Procedure)是为了完成特定功能的SQLt语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行,类似于编程语言的函数或方法。 存储过程的优缺点: #2...

    mysql数据库的基本操作语法

    MySQL可以使用check约束,但check约束对数据验证没有任何作用。 create table temp( id int auto_increment, name varchar(20), age int, primary key(id), /*check约束*/ check(age > 20) ); 上面check约束要求age...

    MySQL数据库:流程控制语句if.pptx

    数据库编程 流程控制语句if 课程目标 1)理解 —— if语句的...说明:存储过程中K1和K2是输入参数,K3是输出参数。 call compar(5,8,@k); select @k; 要比较的数存入K1,K2 “大于”=>K3 K1>K2? K1=K2? “等于”=>K3

    java实验报告:实验六.doc

    输出参数(OUT):在调用一个存储过程时,可用setXXX方法传递输入参数,使用输出参 数接收返回结果。在使用时,必须先调用CallableStatement.registerOutParameter方 法为每一个输出参数进行类型注册,然后执行该过程...

    springmybatis

    其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...

    简单的ADO.net数据访问客户端

    使用参数和调用存储过程 // 使用参数 DbParameter parameter = Db.Northwind.CreateParameter(); parameter.DbType = DbType.String; parameter.ParameterName = "CustomerID"; parameter.Value = "ALFKI"; ...

    cmd操作命令和linux命令大全收集

    date /t 、 time /t 使用此参数即“DATE/T”、“TIME/T”将只显示当前日期和时间,而不必输入新日期和时间 set 指定环境变量名称=要指派给变量的字符 设置环境变量 set 显示当前所有的环境变量...

    day019-io笔记和代码.rar

    * 1.InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader。 * 2.InputStreamReader(InputStream in, String charsetName) 创建一个使用指定字符集的InputStreamReader。 ...

    经典全面的SQL语句大全

     9、说明:in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)  10、说明:两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select *...

    jpivot学习总结.doc

    Closure表针对维度表计算distance,Closure表的生产参见例子中的存储过程sp_zycreateorgclosure 例如: 地区" uniqueMembers="true" nameColumn="ORGENTITYNAME" column="ORGENTITYID" parentColumn=...

Global site tag (gtag.js) - Google Analytics