我如何使用select中的变量来调用存储过程?

时间:2013-06-02 04:35:01

标签: mysql

我的问题是这段代码。

DELIMITER $$

DROP PROCEDURE IF EXISTS agregar_abono$$
CREATE PROCEDURE  agregar_abono(IN pid_cliente BIGINT, IN pfecha_abono DATE, IN pmonto_abono FLOAT)
BEGIN 
    DECLARE @idabono BIGINT;

    -- OTHER CODE ...

    SELECT id_abono AS idabono FROM abono WHERE fk_cliente = pid_cliente ORDER BY id_abono DESC LIMIT 1;

    SELECT CONCAT('>', idabono);


    CALL cobrar_abono(pid_cliente, vid_abono);

END $$

两个SELECT返回的过程:

idabono = 52                  --> good! (in the first select)
CONCAT('>', idabono) = null   ---> what?? 

我不知道因为没有将结果存储在此变量中以便在存储过程中使用。我使用AS来存储变量。

要调用的存储过程的标题是:

CREATE PROCEDURE cobrar_abono(IN pid_cliente BIGINT, IN pid_abono BIGINT)

1 个答案:

答案 0 :(得分:0)

你在找这个吗?

...
DECLARE idabono BIGINT;
-- OTHER CODE ...
SET idabono = (SELECT id_abono
                 FROM abono 
                WHERE fk_cliente = pid_cliente 
                ORDER BY id_abono DESC 
                LIMIT 1);

SELECT语句本身可能会替换为

SET idabono = (SELECT MAX(id_abono)
                 FROM abono 
                WHERE fk_cliente = pid_cliente);