Home > Database > Mysql Tutorial > body text

How to use local variables in MySQL stored procedures?

王林
Release: 2023-09-09 17:45:08
forward
708 people have browsed it

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!