
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Variables in MySQL Stored Procedure with DECLARE Keyword
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)
Advertisements