MySQL 存储过程参数的类型与特点

数据库 MySQL
为我们今天主要向大家描述的是MySQL 存储过程参数的3大类型的描述,以及对这三大不同类型参数的作用与特点的详细介绍。

以下的文章主要是对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 存储过程参数)

  1. begin if (id is not null) then set idid = id + 1;   
  2. end if; select id as id_inner;end;  
  3. set @id = 10;call pr_param_in(@id);select @id as id_out;  
  4. mysql> call pr_param_in(@id);  
  5. +----------+| id_inner |+----------+| 11 |+---------+mysql> select @id as id_out;  
  6. +--------+| id_out |+--------+| 10 |+--------+  

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

 

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

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

 

 

  1. drop procedure if exists pr_param_out;  
  2. create procedure pr_param_out( out id int)begin select id as id_inner_1;  

id 初始值为

 

  1. null if (id is not null) then set idid = id + 1;   
  2. select id as id_inner_2;   
  3. else select 1 into id;   
  4. end if;   
  5. select id as id_inner_3;  
  6. end;  
  7. set @id = 10;  
  8. call pr_param_out(@id);  
  9. select @id as id_out;  
  10. mysql> set @id = 10;  
  11. mysql>mysql> call pr_param_out(@id);  
  12. +------------+| id_inner_1 |+------------+| NULL |+------------++------------+  
  13. | id_inner_3 |+------------+| 1 |+------------+mysql> select @id as id_out;  
  14. +--------+| id_out |+--------+| 1 |+--------+  

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

 

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

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

 

 

  1. drop procedure if exists pr_param_inout;create procedure pr_param_inout( inout id int)begin select id as id_inner_1;  

id 值为调用者传进来的值

  1. if (id is not null) then set idid = id + 1;   
  2. select id as id_inner_2;  
  3. else select 1 into id;   
  4. end if; select id as id_inner_3;end;  
  5. set @id = 10;call pr_param_inout(@id);select @id as id_out;   
  6. mysql> set @id = 10;  
  7. mysql>mysql> call pr_param_inout(@id);  
  8. +------------+| id_inner_1 |+------------+| 10 |+------------++------------+| id_inner_2 |  
  9. +------------+| 11 |+------------++------------+| id_inner_3 |+------------+  
  10. | 11 |+------------+mysql>mysql> select @id as id_out;  
  11. +--------+| id_out |+--------+| 11 |+--------+  

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

 

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

以上的相关内容就是对MySQL 存储过程参数有三种类型:in、out、inout的介绍,望你能有所收获。

【编辑推荐】

  1. MySQL 常用语句的基本用法的介绍
  2. 甲骨文对MySQL数据库做出十大承诺!
  3. 解决MySQL无法远程访问的3方案
  4. PHP和MySQL存储过程的实例演示
  5. linux下MySQL源码如何进行纯手动安装?
责任编辑:佚名 来源: cnblogs
相关推荐

2010-10-08 16:55:44

MySql存储过程

2010-05-21 10:58:19

MySQL存储引擎

2010-10-09 16:58:59

MYSQL存储过程

2010-04-16 12:38:37

Oracle集合类型

2011-08-24 16:56:54

OracleArray类型存储过程

2018-04-04 09:26:40

存储负载应用

2010-04-16 11:39:56

Oracle存储过程

2020-11-26 10:33:44

MySQL存储函数

2021-10-15 06:43:11

数据库存储过程

2019-01-02 13:03:53

MySQL存储权限

2010-06-01 15:09:55

MySQL 存储过程

2010-05-19 14:03:41

MySQL 存储过程

2018-04-18 09:18:44

数据库MySQL存储过程

2010-10-09 16:26:59

mysql存储过程

2010-06-04 14:18:10

MySQL 分页存储过

2010-11-12 09:18:13

SQL Server存

2019-06-03 15:15:09

MySQL索引数据库

2023-07-27 07:03:24

MySQL存储SQL

2009-01-19 08:59:04

PHP调用MySQL存储过程MySQLi扩展

2020-11-02 13:24:49

MySQL数据库存储
点赞
收藏

51CTO技术栈公众号