Local Variables in MySQL Stored Procedure



Local variables are those variables that are declared within the stored procedure. They are only valid within the BEGIN…END block where they are declared and can have any SQL data type. To demonstrate it, we are creating the following procedure −

mysql> DELIMITER // ;
mysql> Create Procedure Proc_Localvariables()
   -> BEGIN
   -> DECLARE X INT DEFAULT 100;
   -> DECLARE Y INT;
   -> DECLARE Z INT;
   -> DECLARE A INT;
   -> SET Y = 250;
   -> SET Z = 200;
   -> SET A = X+Y+Z;
   -> SELECT X,Y,Z,A;
   -> END //
Query OK, 0 rows affected (0.00 sec)

mysql> Delimiter ; //
mysql> CALL Proc_Localvariables();
+------+------+------+------+
| X    | Y    | Z    | A    |
+------+------+------+------+
| 100  | 250  | 200  | 550  |
+------+------+------+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)
Updated on: 2020-06-22T05:33:29+05:30

423 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements