How to correctly implement END IF statement in a MySQL Stored Procedure?


The following is the syntax of END IF statement in MySQL

IF yourCondition THEN
   yourStatement
ELSE
   yourStatement
END IF

Here is the demo of END IF statement while creating a stored procedure

mysql> DELIMITER //
mysql> CREATE PROCEDURE Sp_Test( IN value INT )
   - > BEGIN
   - > IF value < 10 THEN
   - > select 'Your value is less than 10';
   - > ELSE 
   - > select 'Your value is greater than 10';
   - > END IF;
   - >
   - > END //
Query OK, 0 rows affected (1.47 sec)
mysql> DELIMITER ;

Call the stored procedure with the help of CALL command.

The syntax is as follows

CALL yourStoredProcedureName();

Now you can call the above stored procedure as shown below

mysql> CALL Sp_Test(15);

The following is the output

+-------------------------------+
| Your value is greater than 10 |
+-------------------------------+
| Your value is greater than 10 |
+-------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)

Updated on: 30-Jul-2019

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements