MySQL 存储过程参数用法 in, out, inout
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 存储过程参数用法 in, out, inout的更多相关文章
- MySQL 存储过程参数IN OUT INOUT区别
MySQL 存储过程参数IN OUT INOUT对比 一.IN -- 创建测试存储过程 delimiter // create procedure p_in ( IN num int ) begin ...
- MySQL 存储过程参数
MySQL 存储过程参数 MySQL存储过程参数简介 在现实应用中,开发的存储过程几乎都需要参数.这些参数使存储过程更加灵活和有用. 在MySQL中,参数有三种模式:IN,OUT或INOUT. IN ...
- mysql存储过程简单用法
show procedure status 查看所有存储过程 <!-- 简单存储过程 --> 先将结束符改成// delimiter // create procedure query ...
- 超详细讲解mysql存储过程中的in/out/inout
存储过程 大概定义:用一个别名来描述多个sql语句的执行过程. 最简单 delimiter // create PROCEDURE p1() begin select * from userinfo; ...
- MySQL 存储过程传参之in, out, inout 参数用法
存储过程传参:存储过程的括号里,可以声明参数. 语法是 create procedure p([in/out/inout] 参数名 参数类型 ..) in :给参数传入值,定义的参数就得到了值 ou ...
- MYSQL存储过程中的IN、OUT和INOUT
MYSQL存储过程中的IN.OUT和INOUT,不能简单理解为一个方法的参数和返回值,而是面向整个过程上下文变量的. 一.MySQL 存储过程参数(in) 基本可以理解为传入function的参数,而 ...
- mysql存储过程 OUT or INOUT argument 3 for routine
mysql存储过程出现: OUT or INOUT argument 3 for routine gotask.UserLogin is not a variable or NEW pseudo-va ...
- Mysql存储过程调用
mysql存储过程实例教程 发布时间:2014-04-09编辑:JB01 这篇文章主要介绍了mysql存储过程的使用方法,mysql存储过程实例教程,有需要的朋友参考下. 1.1create p ...
- MySQL存储过程(转载)
转自:http://www.blogjava.net/sxyx2008/archive/2009/11/24/303497.html 1.1 CREATE PROCEDURE (创 ...
随机推荐
- mysql联合其他表做更新
在sql server中,我们可是使用以下update语句对表进行更新: update a set a.xx= (select yy from b) where a.id = b.id ; 但是在my ...
- JPG PNG GIF 的优缺点
pg/jpeg:优点:体积比png小,打开速度比png快缺点:属于有损压缩,每次保存都会产生数据损失(jpeg artifacts), 故不适合用于多次编辑保存.压缩率过高图像会失真PS:网络上最最常 ...
- 使用tensorflow 构建rnn网络
使用tensorflow实现了简单的rnn网络用来学习加法运算. tensorflow 版本:1.1 import tensorflow as tf from tensorflow.contrib i ...
- php注册审查
思路 用户注册后就有该条用户记录,你对用户表设一个“审核状态”字段,默认值设为“未审核”,然后你写几句审核代码做成一个功能,按照你们的意愿若审核通过你把审核状态改为“已审核”就行了.用户想进行各种操作 ...
- Eclipse下创建Maven项目(转)
原文出自:http://www.cnblogs.com/hongwz/p/5456616.html 1.新建Maven项目 1.1 File -> New -> Other 1.2 选择M ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 开发者应该了解的API技术清单
近几年,API经济纷纷崛起,无论是国外还是国内,众多厂商积极开放API.开发者很多时候是要借助这些API,才能轻松构建出一款应用,极大地提高开发效率和开发质量.文中整理了一份API服务清单,内容涵盖: ...
- 关于TOSCA自动化测试工具, 我想问一些问题(持续整理中)
通过学习,实践踩坑,有以下问题不太明白 1. Artifacts and results from your complete test portfolio (cross-browser, mobi ...
- 好的博客参考之Spring
https://blog.csdn.net/bao19901210/article/details/41724355
- ARKit 研究笔记一
软件需求:Xcode9.x .blender 硬件需求:iphone 6s + 系统:iOS 11 + 技能储备: ARKit .SceneKit(苹果提供的3d游戏库) 或 SpriteKit(苹果 ...