Nested blocks : Variable Scope « Procedure Function « SQL / MySQL





Nested blocks

 
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>

        










Related examples in the same category

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