
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to declare a variable inside a procedure in MySQL?
You can use the DECLARE command to declare a variable inside a MySQL procedure. Let us create a stored procedure in MySQL −
mysql> DELIMITER // mysql> CREATE PROCEDURE DECLARE_VARIABLE_DEMO(IN value int) -> BEGIN -> DECLARE searchValue int; -> set searchValue=value; -> if searchValue=10 then -> select searchValue+100; -> else -> select searchValue; -> end if; -> END -> // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;
Above, we have declared a variable. Now, let us call the stored procedure using CALL command −
mysql> call DECLARE_VARIABLE_DEMO(10);
This will produce the following output −
+-----------------+ | searchValue+100 | +-----------------+ | 110 | +-----------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.02 sec)
- Related Articles
- How to declare a variable in MySQL?
- MySQL how to declare a datetime variable?
- How to declare a variable in MySQL for a normal query?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How to declare a variable in C++?
- How to declare a variable in Python?
- How to declare a global variable in C++
- How to declare a local variable in Java?
- How to declare a global variable in PHP?
- How to declare a global variable in Python?
- Using DECLARE to create variable in MySQL?
- How to declare a variable correctly in a MySQLProcedure?\n
- How can we handle a result set inside MySQL stored procedure?
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- Implement Dynamic SQL query inside a MySQL stored procedure?

Advertisements