Variable scope : Variable Scope « Procedure Function « SQL / MySQL






Variable scope

 
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>

        








Related examples in the same category

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