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

[sql]  view plain  copy
  1. MySQL存储过程中有INOUT,INOUT类型  
  2. -----------------------------------  
  3. ## IN   IN参数只用来向过程传递信息,为默认值。  
  4. ## MySQL存储过程"in"参数:跟C语言的函数参数的值传递类似,MySQL存储过程内部可能会修改此参数,  
  5. ## 但in类型参数的修改对调用者(caller)来说是不可见的(not visible)  
  6. mysql>use test;  
  7. mysql> drop procedure if exists pr_param_in;  
  8. Query OK, 0 rows affected, 1 warning (0.01 sec)  
  9. mysql> delimiter //  
  10. mysql> create procedure pr_param_in(in id int)  
  11.     -> begin  
  12.     -> if (id is not nullthen  
  13.     ->     set id=id+1;  
  14.     -> end if;  
  15.     -> select id as id_inner;  
  16.     -> end;  
  17.     -> //  
  18. Query OK, 0 rows affected (0.03 sec)  
  19. mysql> delimiter ;  
  20. mysql> set @id=10;  
  21. Query OK, 0 rows affected (0.00 sec)  
  22. mysql> call pr_param_in(@id);  
  23. +----------+  
  24. | id_inner |  
  25. +----------+  
  26. |       11 |  
  27. +----------+  
  28. 1 row in set (0.00 sec)  
  29. Query OK, 0 rows affected (0.00 sec)  
  30. mysql> select @id as id_out;  
  31. +--------+  
  32. | id_out |  
  33. +--------+  
  34. |     10 |  
  35. +--------+  
  36. 1 row in set (0.00 sec)  
  37. ##  可以看到用户变量@id传入值为10,执行存储过程后,在过程内部值为:11(id_inner),  
  38. ##  但外部变量值依旧为:10(id_out)  



[sql]  view plain  copy
  1. <pre name="code" class="sql">==================================================================================  
  2. ## OUT   OUT参数只用来从过程传回信息。  
  3. ## MySQL存储过程"out"参数:从存储过程内部传值给调用者。  
  4. ## 在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。  
  5. mysql> drop procedure if exists pr_param_out;  
  6. Query OK, 0 rows affected, 1 warning (0.01 sec)  
  7. mysql> delimiter //  
  8. mysql> create procedure pr_param_out(out id int)  
  9.     -> begin  
  10.     -> select id as id_inner_1;  
  11.     -> if (id is not nullthen  
  12.     ->     set id=id+1;  
  13.     ->     select id as id_inner_2;  
  14.     -> else  
  15.     ->     select 1 into id;  
  16.     -> end if;  
  17.     -> select id as id_inner_3;  
  18.     -> end;  
  19.     -> //  
  20. Query OK, 0 rows affected (0.01 sec)  
  21. mysql> delimiter ;  
  22. mysql> set @id=10;  
  23. Query OK, 0 rows affected (0.00 sec)  
  24. mysql> call pr_param_out(@id);  
  25. +------------+  
  26. | id_inner_1 |  
  27. +------------+  
  28. |       NULL |  
  29. +------------+  
  30. 1 row in set (0.01 sec)  
  31. +------------+  
  32. | id_inner_3 |  
  33. +------------+  
  34. |          1 |  
  35. +------------+  
  36. 1 row in set (0.01 sec)  
  37. Query OK, 0 rows affected (0.01 sec)  
  38. mysql> select @id as id_out;  
  39. +--------+  
  40. | id_out |  
  41. +--------+  
  42. |      1 |  
  43. +--------+  
  44. 1 row in set (0.00 sec)  
  45. ## 可以看出,虽然我们设置了用户定义变量@id为10,传递@id给存储过程后,在存储过程内部,  
  46. ## id的初始值总是 null(id_inner_1)。最后id值(id_out=1)传回给调用者。  
  47. ===================================================================================  
  48. ## INOUT INOUT参数可以向过程传递信息,如果值改变,则可再从过程外调用。  
  49. ## MySQL存储过程"inout"参数跟out类似,都可以从存储过程内部传值给调用者。  
  50. ## 不同的是:调用者还可以通过inout参数传递至给存储过程。  
  51. mysql> drop procedure if exists pr_param_inout;  
  52. Query OK, 0 rows affected, 1 warning (0.01 sec)  
  53. mysql> delimiter //  
  54. mysql> create procedure pr_param_inout(inout id int)  
  55.     -> begin  
  56.     -> select id as id_inner_1;  
  57.     -> if (id is not nullthen  
  58.     ->     set id=id+1;  
  59.     ->     select id as id_inner_2;  
  60.     -> else  
  61.     ->     select 1 into id;  
  62.     -> end if;  
  63.     -> select id as id_inner_3;  
  64.     -> end;  
  65.     -> //  
  66. Query OK, 0 rows affected (0.01 sec)  
  67. mysql> delimiter ;  
  68. mysql> set @id=10;  
  69. Query OK, 0 rows affected (0.00 sec)  
  70. mysql> call pr_param_inout(@id);  
  71. +------------+  
  72. | id_inner_1 |  
  73. +------------+  
  74. |         10 |  
  75. +------------+  
  76. 1 row in set (0.00 sec)  
  77. +------------+  
  78. | id_inner_2 |  
  79. +------------+  
  80. |         11 |  
  81. +------------+  
  82. 1 row in set (0.00 sec)  
  83. +------------+  
  84. | id_inner_3 |  
  85. +------------+  
  86. |         11 |  
  87. +------------+  
  88. 1 row in set (0.01 sec)  
  89. Query OK, 0 rows affected (0.01 sec)  
  90. mysql> select @id as id_out;  
  91. +--------+  
  92. | id_out |  
  93. +--------+  
  94. |     11 |  
  95. +--------+  
  96. 1 row in set (0.00 sec)  
  97. ## 从结果可以看出:我们把 @id(10)传给存储过程后,存储过程最后又把计算结果值11(id_inner_3)  
  98. ## 传回给调用者。MySQL存储过程inout参数的行为跟C语言函数中的引用传值类似。  
  99. =========================================================================================  
  100. 通过以上例子:  
  101. 1)  如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;  
  102. 2)  如果仅仅从MySQL存储过程返回值,那就用out类型参数;  
  103. 3)  如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值