Implement If else in stored procedure in MySQL?


To implement if-else, the syntax is as follows −

if yourCondition then
     yourStatement1;
    else
    yourStatement2;
    end if ;

To understand the above concept for if-else in a stored procedure, let us create a stored procedure −

mysql> delimiter //
mysql> create procedure If_else_stored_demo(value int)
     begin
     if value > 1000 then
     select "your value is greater than 1000";
     else
     select "your value is less than or equal to 1000";
     end if ;
     end
     //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

Now you can call the stored procedure using call command −

mysql> call If_else_stored_demo(500);

This will produce the following output −

+------------------------------------------+
| your value is less than or equal to 1000 |
+------------------------------------------+
| your value is less than or equal to 1000 |
+------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

Updated on: 24-Dec-2019

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements