Home >Database >Mysql Tutorial >Using IF logic to call a stored procedure within a stored procedure?

Using IF logic to call a stored procedure within a stored procedure?

WBOY
WBOYforward
2023-08-29 11:05:021422browse

使用 IF 逻辑在存储过程中调用存储过程?

To call a stored procedure 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. 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 the stored procedure using 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)

The above is the detailed content of Using IF logic to call a stored procedure within a stored procedure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:DBMS interview FAQsNext article:DBMS interview FAQs