
- 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
Set conditions in a MySQL stored procedure
To set conditions in a stored procedure, use IF...ELSE in MySQL. Following is the syntax for if-else −
IF yourCondition then yourStatement1, ELSE yourStatement2, END IF;
Let us implement the above syntax in a stored procedure −
mysql> DELIMITER // mysql> CREATE PROCEDURE IF_ELSE_DEMO(IN value int) -> BEGIN -> SET @val=value; -> IF @val > 10 then -> select concat(@val,' is greater than 10'); -> ELSE -> select concat(@val,' is less than 10 '); -> END IF; -> END; -> // Query OK, 0 rows affected (0.16 sec)
mysql> DELIMITER ;
Now you can call the stored procedure using CALL command −
mysql> call IF_ELSE_DEMO(18);
This will produce the following output −
+------------------------------------+ | concat(@val,' is greater than 10') | +------------------------------------+ | 18 is greater than 10 | +------------------------------------+ 1 row in set (0.03 sec) Query OK, 0 rows affected (0.04 sec)
- Related Articles
- How to correctly implement conditions in MySQL stored procedure?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How can we handle a result set inside MySQL stored procedure?
- MySQL stored procedure return value?
- MySQL stored-procedure: out parameter?
- Create a stored procedure with delimiter in MySQL
- Implement Conditional MySQL Query in a stored procedure?
- Perform mathematical operations in a MySQL Stored Procedure?
- How to set two variables in a stored procedure with a single MySQL select statement?
- MySQL Stored Procedure to create a table?
- Working with WHERE IN() in a MySQL Stored Procedure
- Insert data in a table in MySQL stored procedure?
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- View stored procedure/function definition in MySQL?
- Implement DELETE query in MySQL stored procedure

Advertisements