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;

二、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;

三、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;

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

总结

更多相关文章

  1. MySQL系列多表连接查询92及99语法示例详解教程
  2. 《Android开发从零开始》——25.数据存储(4)
  3. Android(安卓)- Manifest 文件 详解
  4. Android的Handler机制详解3_Looper.looper()不会卡死主线程
  5. Selector、shape详解(一)
  6. android2.2资源文件详解4--menu文件夹下的菜单定义
  7. Android发送短信方法实例详解
  8. Android(安卓)读取资源文件实例详解
  9. 详解Android中的屏幕方向

随机推荐

  1. Android入门之Activity的四种启动模式
  2. Android源代码编译脚本
  3. Android 常用控件(四)
  4. Android常用的网路框架
  5. android 数据持久化简述
  6. 【Android 初学】11、关于Android当中的
  7. Android IPC 通讯机制源码分析
  8. android关机重启流程代码
  9. Rexsee API介绍:Android屏幕锁定Keyguard
  10. Android获取手机信号强度/信号格数