MySQL:在另一个存储过程中调用存储过程时获取返回码?

时间:2012-09-12 16:52:34

标签: mysql sql stored-procedures

您有2个MySQL存储过程,两者都在最后选择返回码。您需要在另一个内部调用1个存储过程,并获取其返回代码。这可能吗?

过程Proc1:

CREATE PROCEDURE (IN...)
BEGIN
DECLARE ret_code
...UPDATE SOMETHING....
SELECT ret_code as return_code from dual;
END

PROC2:

CREATE PROCEDURE (IN...)
BEGIN
DECLARE returnVal
if(conditional true)
    ..Update something else..
    Set returnVal = x;
else
    call proc1(var1,...)
    Set returnVal = (ret_code obtained from proc1)
end if
select returnVal;
END

在proc2中调用proc1时,如何获取在proc2中proc1结束时选择的ret_code?

1 个答案:

答案 0 :(得分:0)

您应该使用CREATE FUNCTION代替CREATE PROCEDURE

CREATE FUNCTION (...) RETURNS ...datatype...
BEGIN
DECLARE ret_code
...UPDATE SOMETHING....
RETURN ret_code;
END

CREATE FUNCTION (...) RETURNS ...datatype...
BEGIN
DECLARE returnVal
if(conditional true)
    ..Update something else..
    Set returnVal = x;
else
    Set returnVal = proc1(var1,...)
end if
RETURN returnVal;
END

或者为过程指定OUT参数,并使用:

CREATE PROCEDURE (OUT ret_code ...datatype..., IN...)
BEGIN
...UPDATE SOMETHING....
Set ret_code = return_code;
END

CREATE PROCEDURE (OUT returnVal ...datatype..., IN...)
BEGIN
if(conditional true)
    ..Update something else..
    SET returnVal = x;
else
    call proc1(returnVal, var1,...)
end if
END