- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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)
- Related Articles
- Create a stored procedure with delimiter in MySQL
- How can I create MySQL stored procedure with IN parameter?
- How can local variables be used in MySQL stored procedure?
- How can user variables be used in MySQL stored procedure?
- How can I create MySQL stored procedure with OUT parameter?
- How can I create MySQL stored procedure with INOUT parameter?
- MySQL Stored procedure to declare two values and perform mathematical operation
- MySQL Stored Procedure to create a table?
- MySQL stored procedure to execute SHOW CREATE TABLE?
- How to set two variables in a stored procedure with a single MySQL select statement?
- What is stored procedure and how can we create MySQL stored procedures?
- What do you mean by Scope of variables inside MySQL stored procedure?
- Working with WHERE IN() in a MySQL Stored Procedure
- Create a MySQL stored procedure that generates five random numbers?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?

Advertisements