Inner variable shadows the outter variable : Variable Scope « Procedure Function « SQL / MySQL






Inner variable shadows the outter variable

 
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.00 sec)

mysql>
mysql> delimiter ;
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.00 sec)

mysql>
mysql>

        








Related examples in the same category

1.LOCAL, SESSION, AND GLOBAL VARIABLES IN MYSQL
2.Variable scope
3.Nested blocks
4.Using Declare statement to declare the local variable