Calling a Stored Procedure in a Stored Procedure in MySQL

Solution 1:

CREATE PROCEDURE innerproc(OUT param1 INT)
BEGIN
 insert into sometable;
 SELECT LAST_INSERT_ID() into param1 ;
END
-----------------------------------
CREATE PROCEDURE outerproc()
BEGIN
CALL innerproc(@a);
// @a gives you the result of innerproc
SELECT @a INTO variableinouterproc FROM dual;
END

OUT parameters should help you in getting the values back to the calling procedure.Based on that the solution must be something like this.

Solution 2:

To call another procedure, use CALL: ex: Call SP1(parm1, parm2);

To get identity, did you try checking out LAST_INSERT_ID(); You would do something like SELECT LAST_INSERT_ID() after your SP call.

Here's a complete, tested example:

DELIMITER $$

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) $$

CREATE PROCEDURE sp1()
BEGIN
  insert into animals (name) values ('bear');
END $$

CREATE PROCEDURE sp2()
BEGIN
  call sp1;
  select last_insert_id();
END $$

call sp2;