Variable scope : Variable Scope « Procedure Function « MySQL Tutorial






mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->         DECLARE outer_variable VARCHAR(20);
    ->         BEGIN
    ->                 DECLARE inner_variable VARCHAR(20);
    ->                 SET inner_variable='This is my private data';
    ->         END;
    ->         SELECT inner_variable,' This statement causes an error ';
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call myProc();
ERROR 1054 (42S22): Unknown column 'inner_variable' in 'field list'
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>








11.11.Variable Scope
11.11.1.LOCAL, SESSION, AND GLOBAL VARIABLES IN MYSQL
11.11.2.Variable scope
11.11.3.Inner variable shadows the outter variable
11.11.4.Nested blocks
11.11.5.Using Declare statement to declare the local variable