Call Stored Procedures within a Stored Procedure with IF Logic?

MySQLMySQLi Database

To call stored procedures within a stored procedure, the syntax is as follows −

If yourInputValue > 100 then
     call yourProcedureName1();
 else
    call yourProcedureName2();
    end If ;
    END

Let us implement the above syntax. In order to implement the above concept, let us create a stored procedure −

mysql> delimiter //
mysql> create procedure Hello_Stored_Procedure()
   -> BEGIN
   -> select 'Hello World!!!';
   -> END
   -> //
Query OK, 0 rows affected (0.18 sec)

The query to create the second stored procedure is as follows −

mysql> create procedure Hi_Stored_Procedure()
   -> BEGIN
   -> select 'Hi!!!';
   -> END
   -> //
Query OK, 0 rows affected (0.17 sec)

Here is the query to call stored procedures within a stored procedure with IF logic −

mysql> DELIMITER //
mysql> create procedure test(IN input int)
   -> BEGIN
   -> If input > 100 then
   -> call Hello_Stored_Procedure();
   -> else
   -> call Hi_Stored_Procedure();
   -> end If ;
   -> END
   -> //
Query OK, 0 rows affected (0.18 sec)

Now you can call the stored procedure with the help of call −

mysql> delimiter ;
mysql> call test(110);

This will produce the following output −

+----------------+
| Hello World!!! |
+----------------+
| Hello World!!! |
+----------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
raja
Published on 16-Dec-2019 11:45:31
Advertisements