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
Release: 2023-08-29 11:05:02
forward
1437 people have browsed it

使用 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
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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

mysql> delimiter ;
mysql> call test(110);
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template