MySQL存储过程中的IN,OUT,INOUT类型

MySQL存储过程中有IN,OUT,INOUT类型

-----------------------------------

## IN   IN参数只用来向过程传递信息,为默认值。

## MySQL存储过程"in"参数:跟C语言的函数参数的值传递类似,MySQL存储过程内部可能会修改此参数,

## 但in类型参数的修改对调用者(caller)来说是不可见的(not visible)

mysql>use test;

mysql> drop procedureifexists pr_param_in;

QueryOK,0rows affected,1warning (0.01sec)

mysql> delimiter //

mysql> create procedure pr_param_in(inid int)

    -> begin

    ->if(id is not null) then

    ->    set id=id+1;

    -> endif;

    -> select idasid_inner;

    -> end;

    -> //

QueryOK,0rows affected (0.03sec)

mysql> delimiter ;

mysql> set @id=10;

QueryOK,0rows affected (0.00sec)

mysql> call pr_param_in(@id); 

+----------+

| id_inner | 

+----------+

|11| 

+----------+

1rowinset (0.00sec)QueryOK,0rows affected (0.00sec)

mysql> select @id as id_out;

 +--------+

| id_out | 

+--------+

|10| 

+--------+

1rowinset (0.00sec)

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

##  但外部变量值依旧为:10(id_out)

==================================================================================

## OUT   OUT参数只用来从过程传回信息。

## MySQL存储过程"out"参数:从存储过程内部传值给调用者。

## 在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。

mysql> drop procedureifexists pr_param_out;

QueryOK,0rows affected,1warning (0.01sec)

mysql> delimiter //

mysql> create procedure pr_param_out(out id int)

    -> begin

    -> select idasid_inner_1; 

   ->if(id is not null) then

   ->    set id=id+1;

    ->    select idasid_inner_2; 

   ->else->    select1into id;

    -> endif;

    -> select idasid_inner_3;

    -> end;

    -> //

QueryOK,0rows affected (0.01sec)

mysql> delimiter ;

mysql> set @id=10;

QueryOK,0rows affected (0.00sec)

mysql> call pr_param_out(@id); 

+------------+

| id_inner_1 | 

+------------+

|NULL| 

+------------+

1rowinset (0.01sec) 

+------------+

| id_inner_3 | 

+------------+

|1|

 +------------+

1rowinset (0.01sec)

QueryOK,0rows affected (0.01sec)

mysql> select @idasid_out; 

+--------+

| id_out |

+--------+

|1| 

+--------+

1rowinset (0.00sec)

## 可以看出,虽然我们设置了用户定义变量@id为10,传递@id给存储过程后,在存储过程内部,

## id的初始值总是 null(id_inner_1)。最后id值(id_out=1)传回给调用者。

===================================================================================

## INOUT INOUT参数可以向过程传递信息,如果值改变,则可再从过程外调用。

## MySQL存储过程"inout"参数跟out类似,都可以从存储过程内部传值给调用者。

## 不同的是:调用者还可以通过inout参数传递至给存储过程。

mysql> drop procedureifexists pr_param_inout;

QueryOK,0rows affected,1warning (0.01sec)

mysql> delimiter //

mysql> create procedure pr_param_inout(inout id int)

    -> begin

    -> select idasid_inner_1;

    ->if(id is not null) then

    ->    set id=id+1;

    ->    select idasid_inner_2;

    ->else

    ->    select 1 into id;

    -> endif;

    -> select id as id_inner_3;

    -> end;

    -> //

QueryOK,0rows affected (0.01sec)

mysql> delimiter ;

mysql> set @id=10;

QueryOK,0rows affected (0.00sec)

mysql> call pr_param_inout(@id);

 +------------+

| id_inner_1 | 

+------------+

|10| 

+------------+

1rowinset (0.00sec) 

+------------+

| id_inner_2 | 

+------------+

|11| 

+------------+

1rowinset (0.00sec) 

+------------+

| id_inner_3 | 

+------------+

|11| 

+------------+

1rowinset (0.01sec)QueryOK,0rows affected (0.01sec)

mysql> select @idasid_out; 

+--------+

| id_out | 

+--------+

|11| 

+--------+

1rowinset (0.00sec)

## 从结果可以看出:我们把 @id(10)传给存储过程后,存储过程最后又把计算结果值11(id_inner_3)

## 传回给调用者。MySQL存储过程inout参数的行为跟C语言函数中的引用传值类似。

=========================================================================================

通过以上例子:

1)  如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;

2)  如果仅仅从MySQL存储过程返回值,那就用out类型参数;

3)  如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。

原文地址:http://blog.sina.com.cn/s/blog_6238358c0100n7ss.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,769评论 18 399
  • 任务需求:定时执行的任务,调用存储过程,进行数据迁移。 存储过程相关总结:(存储过程的创建 不能伴随有if exi...
    时待吾阅读 3,129评论 0 4
  • 转载自这里 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Sto...
    杜七阅读 2,423评论 4 27
  • 原文链接 MySQL存储过程详解 1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然...
    亚斯咪妮阅读 2,689评论 1 30
  • article 一个故事告诉你比特币的原理及运作机制 The Proof-of-Work Concept 比特币白...
    dyun阅读 479评论 0 2