登录 |  注册 |  繁體中文


MySQL存储过程参数类型:in、out、inout详解

分类: 数据库 颜色:橙色 默认  字号: 阅读(2193) | 评论(0)
一、MySQL 存储过程参数(in)
MySQL 存储过程 “in” 参数:跟 C 语言的函数参数的值传递类似, MySQL 存储过程内部可能会修改此参数,但对 in 类型参数的修改,对调用者(caller)来说是不可见的(not visible)。
 
  1. drop procedure if exists pr_param_in;  
  2.   
  3. create procedure pr_param_in  
  4. (  
  5.    in id int -- in 类型的 MySQL 存储过程参数  
  6. )  
  7. begin  
  8.    if (id is not nullthen  
  9.       set id = id + 1;  
  10.    end if;  
  11.   
  12.    select id as id_inner;  
  13. end;  


 
  1. set @id = 10;  
  2. call pr_param_in(@id);  
  3. select @id as id_out;  


 
  1. mysql> call pr_param_in(@id);  
  2. +----------+  
  3. | id_inner |  
  4. +----------+  
  5. |       11 |  
  6. +----------+  
  7.   
  8. mysql> select @id as id_out;  
  9. +--------+  
  10. | id_out |  
  11. +--------+  
  12. | 10     |  
  13. +--------+  


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

二、MySQL 存储过程参数(out)
MySQL 存储过程 “out” 参数:从存储过程内部传值给调用者。在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。
 
  1. drop procedure if exists pr_param_out;  
  2.   
  3. create procedure pr_param_out  
  4. (  
  5.    out id int  
  6. )  
  7. begin  
  8.    select id as id_inner_1;  -- id 初始值为 null  
  9.   
  10.    if (id is not nullthen  
  11.       set id = id + 1;  
  12.   
  13.       select id as id_inner_2;  
  14.    else  
  15.       select 1 into id;  
  16.    end if;  
  17.   
  18.    select id as id_inner_3;  
  19. end;  


 
  1. set @id = 10;  
  2.   
  3. call pr_param_out(@id);  
  4.   
  5. select @id as id_out;  

 
  1. mysql> set @id = 10;  
  2. mysql>  
  3. mysql> call pr_param_out(@id);  
  4. +------------+  
  5. | id_inner_1 |  
  6. +------------+  
  7. |       NULL |  
  8. +------------+  
  9.   
  10. +------------+  
  11. | id_inner_3 |  
  12. +------------+  
  13. |          1 |  
  14. +------------+  
  15.   
  16. mysql> select @id as id_out;  
  17. +--------+  
  18. | id_out |  
  19. +--------+  
  20. | 1      |  
  21. +--------+  


可以看出,虽然我们设置了用户定义变量 @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;  
  2.   
  3. create procedure pr_param_inout  
  4. (  
  5.    inout id int  
  6. )  
  7. begin  
  8.    select id as id_inner_1;  -- id 值为调用者传进来的值  
  9.   
  10.    if (id is not nullthen  
  11.       set id = id + 1;  
  12.   
  13.       select id as id_inner_2;  
  14.    else  
  15.       select 1 into id;  
  16.    end if;  
  17.   
  18.    select id as id_inner_3;  
  19. end;  


 
  1. set @id = 10;  
  2.   
  3. call pr_param_inout(@id);  
  4.   
  5. select @id as id_out;  


 
  1. mysql> set @id = 10;  
  2. mysql>  
  3. mysql> call pr_param_inout(@id);  
  4. +------------+  
  5. | id_inner_1 |  
  6. +------------+  
  7. |         10 |  
  8. +------------+  
  9.   
  10. +------------+  
  11. | id_inner_2 |  
  12. +------------+  
  13. |         11 |  
  14. +------------+  
  15.   
  16. +------------+  
  17. | id_inner_3 |  
  18. +------------+  
  19. |         11 |  
  20. +------------+  
  21. mysql>  
  22. mysql> select @id as id_out;  
  23. +--------+  
  24. | id_out |  
  25. +--------+  
  26. | 11     |  
  27. +--------+  

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

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


上一篇:js操作framesetframe对象   下一篇:squid配置

姓 名: *
邮 箱:
内 容: *
验证码: 点击刷新 *   

回到顶部