关于mysql存储过程变量的问题?变量什么时候前面要加@符号,用declare声明的变量与前者有和区别,我不懂?

解决方案 »

  1.   

    前面加@代表会话变量,作用域是当前登录的账户,declare申明的变量只在存储过程内部有效。
    通过下面的例子你就能明白:mysql> create procedure pro1()
        -> begin
        -> declare d int;
        -> select max(id) into d from o;
        -> set @dd=0;
        -> select max(id) into @dd from o;
        -> end;
        -> //
    Query OK, 0 rows affected (0.05 sec)mysql> call pro1;
        -> //
    Query OK, 0 rows affected (0.00 sec)mysql> select @dd;
        -> //
    +------+
    | @dd  |
    +------+
    |    2 |
    +------+
    1 row in set (0.00 sec)mysql> select d//
    ERROR 1054 (42S22): Unknown column 'd' in 'field list'