在 MySQL 中的存储过程中调用存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11000802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:47:32  来源:igfitidea点击:

Calling a Stored Procedure in a Stored Procedure in MySQL

mysqlstored-procedures

提问by Mike Flynn

I can't find this answer anywhere, but can you call a Stored Procedure from another Stored Procedure in MySQL? I want to get the Identity Value back and use it in the parent Stored Procedure. We can't use FUNCTIONS anymore!

我在任何地方都找不到这个答案,但是您可以从 MySQL 中的另一个存储过程调用存储过程吗?我想取回标识值并在父存储过程中使用它。我们不能再使用 FUNCTIONS 了!

回答by Ahamed Mustafa M

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

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

OUT参数应该可以帮助您将值返回给调用过程。基于此,解决方案必须是这样的。

回答by dcp

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

要调用另一个过程,请使用CALL:例如: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.

为了获得身份,您是否尝试查看LAST_INSERT_ID();你会SELECT LAST_INSERT_ID()在你的 SP 通话后做一些类似的事情。

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;