
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can a MySQL stored procedure call another MySQL stored procedure inside it?
It is quite possible that a MySQL stored procedure can call another MySQL stored procedure inside it. To demonstrate it, we are taking an example in which a stored procedure will call another stored procedure to find out the 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 1st 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() then it inserted the first value in the table called employee.tbl and when we select last_insert_id() in 2nd stored procedure i.e. insert2() then it gives the output 1.
- Related Articles
- How can we perform START transactions inside MySQL stored procedure?
- How can we perform COMMIT transactions inside MySQL stored procedure?
- How can we handle a result set inside MySQL stored procedure?
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- How can we invoke MySQL stored procedure?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- Implement Dynamic SQL query inside a MySQL stored procedure?
- Can we call stored procedure recursively?
- How to call a stored procedure using select statement in MySQL?
- MySQL stored-procedure: out parameter?
- MySQL stored procedure return value?
- What is stored procedure and how can we create MySQL stored procedures?
- How can we write MySQL handler in a stored procedure?
- How to suppress MySQL stored procedure output?

Advertisements