How to correctly implement conditions in MySQL stored procedure?


To set conditions in stored procedure, use the below syntax −

    if yourCondition then
   yourStatement1;
     else
   yourStatement2';
      end if ;
    end
    //

Let us implement the above syntax in order to correct missing semicolon in stored procedure −

mysql> delimiter //
mysql> create procedure Test_Demo(In inputValue int)
   -> BEGIN
   -> if inputValue=10 then
   -> select 'You have won 100$';
   -> else
   -> select 'Sorry !!!';
    -> end if ;
    -> end
    -> //
Query OK, 0 rows affected (0.20 sec)
mysql> delimiter ;

Now you can call stored procedure using CALL command −

mysql> call Test_Demo(10);

This will produce the following output −

+-------------------+
| You have won 100$ |
+-------------------+
| You have won 100$ |
+-------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)

Updated on: 16-Dec-2019

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements