Nested blocks : Variable Scope « Procedure Function « MySQL Tutorial






mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->         DECLARE my_variable VARCHAR(20);
    ->         SET my_variable='This value was set in the outer block';
    ->         BEGIN
    ->                 DECLARE my_variable VARCHAR(20);
    ->                 SET my_variable='This value was set in the inner block';
    ->         END;
    ->         SELECT my_variable, 'Can''t see changes made in the inner block';
    -> END$$
Query OK, 0 rows affected (0.02 sec)

mysql> delimiter ;
mysql>
mysql> call myProc();
+----------------------+-------------------------------------------+
| my_variable          | Can't see changes made in the inner block |
+----------------------+-------------------------------------------+
| This value was set i | Can't see changes made in the inner block |
+----------------------+-------------------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> drop procedure myProc;
Query OK, 0 rows affected (0.02 sec)

mysql>
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