
- 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 can MySQL IF ELSEIF ELSE statement be used in a stored procedure?
MySQL IF ELSEIF ELSE execute the statements based on multiple expressions Its syntax is as follows −
IF expression THEN statements; ELSEIF elseif-expression THEN elseif-statements; … … … … ELSE else-statements; END IF;
The statements must end with a semicolon.
To demonstrate the use of IF ELSEIF ELSE statement within MySQL stored procedure, we are creating the following stored procedure which is based on the values, as shown below, of the table named ‘student_info’ −
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 125 | Raman | Shimla | Computers | +------+---------+------------+------------+ 3 rows in set (0.00 sec)
The following query will create a procedure named ‘coursedetails_IF_ELSEIF’ which have IF ELSEIF ELSE statements in it −
mysql> DELIMITER // ; mysql> CREATE PROCEDURE coursedetails_IF_ELSEIF(IN S_Subject Varchar(20), OUT S_Course varchar(20)) -> BEGIN -> DECLARE Sub Varchar(20); -> SELECT Subject INTO SUB -> FROM Student_info WHERE S_Subject = Subject; -> IF Sub = 'Computers' THEN -> SET S_Course = 'B.Tech(CSE)'; -> ELSEIF Sub = 'History' THEN -> SET S_Course = 'Masters in History'; -> ELSEIF Sub = 'Literature' THEN -> SET S_Course = 'Masters in English'; -> END IF; -> END // Query OK, 0 rows affected (0.00 sec)
Now, we can see the result below when we invoke this procedure −
mysql> Delimiter ; // mysql> CALL coursedetails_IF_ELSEIF('Computers', @S_Course); Query OK, 1 row affected (0.00 sec) mysql> Select @S_Course; +-------------+ | @S_Course | +-------------+ | B.Tech(CSE) | +-------------+ 1 row in set (0.00 sec) mysql> CALL coursedetails_IF_ELSEIF ('Literature', @S_Course); Query OK, 1 row affected (0.00 sec) mysql> Select @S_Course; +--------------------+ | @S_Course | +--------------------+ | Masters in English | +--------------------+ 1 row in set (0.00 sec)
- Related Articles
- How MySQL IF ELSE statement can be used in a stored procedure?
- How MySQL IF statement can be used in a stored procedure?
- How Can MySQL LOOP statement be used in a stored procedure?
- How Can MySQL CASE statement be used in stored procedure?
- How MySQL WHILE loop statement can be used in stored procedure?
- How MySQL REPEAT loop statement can be used in stored procedure?
- Implement If else in stored procedure in MySQL?
- How can local variables be used in MySQL stored procedure?
- How can user variables be used in MySQL stored procedure?
- PHP if else elseif
- How to correctly implement END IF statement in a MySQL Stored Procedure?
- IF ELSE statement in a MySQL Statement?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How to call a stored procedure using select statement in MySQL?
- How can we alter a MySQL stored procedure?

Advertisements