≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > MySQL

分析MySQL存储过程参数有三种类型(in、out、inout)

人气:336 时间:2020-07-09

这篇文章主要为大家详细介绍了分析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” 类型参数。

本文来自:http://www.q1010.com/177/10243-0.html

注:关于分析MySQL存储过程参数有三种类型(in、out、inout)的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:MYSQL

您可能感兴趣的文章

  • MySQL unsigned 用法及相减出现补数溢出解决方法
  • 在MySQL字段中使用逗号分隔符的方法分享
  • MySQL的存储过程学习小结 附pdf文档下载
  • MySQL服务1067错误多种解决方案分享
  • Linux下修改MySQL编码的方法
  • MySQL 请选择合适的列
  • MySQL字符集乱码问题解决方法介绍
  • MySQL中迅速插入百万条测试数据的方法
  • MySQL 错误问题汇总(不断更新中)
  • linux下MySQL链接被防火墙阻止的解决方法
上一篇:MySQL中CHAR和VARCHAR类型演变和分析
下一篇:在MySQL字段中使用逗号分隔符的方法分享
热门文章
  • mysql 修改character_set_server为utf-8的简单示例
  • 解决MySQL丢失文件localhost.localdomain.pid、mysql.sock的示例
  • MySQL 数据类型binary和varbinary的简单示例
  • MySQL:reading initial communication packet问题解决方法
  • MySql 表类型MYISAM、InnoDB区别
  • bash: mysql: command not found 的解决方法
  • MYSQL默认用户名ROOT修改方法
  • MySQL 常用命令菜鸟教程
  • MySQL 使用命令行新建用户并授予权限
  • MySql 数据库物理文件存放位置查看示例
  • 最新文章
    • MySQL查看死锁与解除死锁的简单示例
    • MySQL 慢查询的功能实例
    • MySQL查看死锁与去除死锁的简单示例
    • MySQL找出未提交事务的SQL的简单示例
    • MySQL锁阻塞的的简单示例
    • MySQL中的binary类型使用操作的示例
    • SQL优化教程之in与range查询的简单示例
    • MySQL 的 21 个规范、优化最佳实践!
    • MySQL 字符类型大小写敏感的简单示例
    • 解决mybatis-plus分页传入参数后sql where条件没有limit分页信息的问题

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。