Home > Database > Mysql Tutorial > 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?

王林
Release: 2023-09-05 08:45:12
forward
1251 people have browsed it

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

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

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!

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