How can user variables be used in MySQL stored procedure?


In MySQL stored procedure, user variables are referenced with an ampersand i.e. @, prefixed to the user variable names. For example, @A, @B, etc. are user variables. To demonstrate it, we are creating the following procedure −

mysql> DELIMITER // ;
mysql> CREATE PROCEDURE Proc_Uservariables()
   -> BEGIN
   -> SET @A = 100;
   -> SET @B = 500;
   -> SELECT @A,@B,@A+@B;
   -> END //
Query OK, 0 rows affected (0.00 sec)

mysql> Delimiter ; //
mysql> CALL Proc_Uservariables();
+------+------+-------+
| @A   | @B   | @A+@B |
+------+------+-------+
|  100 |  500 |   600 |
+------+------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Updated on: 22-Jun-2020

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements