多次选择MySQL会话变量

时间:2014-04-15 16:17:01

标签: mysql session variables procedures

选择由存储过程更改的会话变量时遇到奇怪的问题。

 call calculate("input1", "input2",@price);
 select @price; /*price is 2.00*/


 call calculate("anotherinput","anotherinput2",@price);
 select @price; /*price is 3.00*/

然而,我得到的答案实际上是2 3.00而不是2.00然后是3.00。假设它被覆盖并搞砸了。但是最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

过程可能未将参数price定义为OUT参数,而是IN参数。除非传递的IN参数始终显示相同的OLD传递值。

工作示例

mysql> create procedure test_sp( inout p int, in q int ) set p := p*q;
mysql> set @p := 4; set @q := 6;
mysql> delimiter //
mysql> call test_sp( @p, @q );
    -> select @p, @q;
    -> call test_sp( @p, @q );
    -> select @p, @q;
    -> //

+------+------+
| @p   | @q   |
+------+------+
|   24 |    6 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| @p   | @q   |
+------+------+
|  144 |    6 |
+------+------+
1 row in set (0.01 sec)
相关问题