MySQL存储过程如何在其中调用另一个MySQL存储过程?

MySQL存储过程很可能可以在其中调用另一个MySQL存储过程。为了说明这一点,我们以一个存储过程将调用另一个存储过程来找出last_insert_id的示例为例。

示例

mysql> Create table employee.tbl(Id INT NOT NULL AUTO_INCREMENT, Name Varchar(30) NOT NULL, PRIMARY KEY(id))//

mysql> Create Procedure insert1()
   -> BEGIN insert into employee.tbl(name) values ('Ram');
   -> END//

现在,在下一个过程insert2()中,我们将调用第一个存储过程,即insert1()。

mysql> Create Procedure insert2()
   -> BEGIN
   -> CALL insert1();
   -> Select last_insert_id();
   -> END //
mysql> Delimiter ;

mysql> Call insert2();
+------------------+
| last_insert_id() |
+------------------+
| 1                |
+------------------+
1 row in set (0.36 sec)

上面的结果集表明,当我们调用insert1()时,它将第一个值插入到名为employee.tbl的表中;当我们在第二个存储过程中选择了last_insert_id()时,即insert2(),则给出了输出1。

猜你喜欢