Home  >  Article  >  Database  >  How to use local variables in MySQL stored procedures?

How to use local variables in MySQL stored procedures?

王林
王林forward
2023-09-09 17:45:08705browse

How to use local variables in MySQL stored procedures?

Local variables are variables declared in a stored procedure. They are only valid within the BEGIN…END block in which they are declared, and can have any SQL data type. To demonstrate it we are creating the following process -

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)

The above is the detailed content of How to use local variables in MySQL stored procedures?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete