Use MySQL DECLARE for variables in stored procedure −
DECLARE anyVariableName int DEFAULT anyValue;
Let us implement the above syntax in order to create variables in stored procedure −
mysql> DELIMITER // mysql> CREATE PROCEDURE variable_Demo() -> BEGIN -> DECLARE lastInsertedId int DEFAULT -1; -> select lastInsertedId; -> set @providedLastId=10001; -> select @providedLastId; -> END -> // Query OK, 0 rows affected (0.32 sec) mysql> DELIMITER ;
Now you can call the above stored procedure using CALL command −
mysql> call variable_Demo();
This will produce the following output −
+----------------+ | lastInsertedId | +----------------+ | -1 | +----------------+ 1 row in set (0.00 sec) +-----------------+ | @providedLastId | +-----------------+ | 10001 | +-----------------+ 1 row in set (0.02 sec) Query OK, 0 rows affected (0.04 sec)