How to correctly implement conditions in MySQL stored procedure?

MySQLMySQLi Database

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)
raja
Published on 16-Dec-2019 12:11:35
Advertisements