Home  >  Article  >  Database  >  How does a MySQL stored procedure call another MySQL stored procedure inside it?

How does a MySQL stored procedure call another MySQL stored procedure inside it?

王林
王林forward
2023-09-05 08:45:121204browse

How does a MySQL stored procedure call another MySQL stored procedure inside it?

A MySQL stored procedure may very well be able to call another MySQL stored procedure within it. To demonstrate this, let's take an example where one stored procedure will call another stored procedure to find out last_insert_id.

Example

mysql> Create table employee.tbl(Id INT NOT NULL AUTO_INCREMENT, Name Varchar(30) NOT NULL, PRIMARY KEY(id))//
Query OK, 0 rows affected (3.87 sec)

mysql> Create Procedure insert1()
   -> BEGIN insert into employee.tbl(name) values ('Ram');
   -> END//
Query OK, 0 rows affected (0.10 sec)

Now, in the next procedure insert2(), we will call the first stored procedure i.e. insert1().

mysql> Create Procedure insert2()
   -> BEGIN
   -> CALL insert1();
   -> Select last_insert_id();
   -> END //
Query OK, 0 rows affected (0.11 sec)
mysql> Delimiter ;

mysql> Call insert2();
+------------------+
| last_insert_id() |
+------------------+
| 1                |
+------------------+
1 row in set (0.36 sec)
Query OK, 0 rows affected (0.37 sec)

The above result set shows that when we call insert1(), it inserts the first value in the table named employee.tbl, and when we call insert1() in the second stored procedure (i.e. insert2 ()), it gives output 1.

The above is the detailed content of How does a MySQL stored procedure call another MySQL stored procedure inside it?. 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