存储过程OUT返回空值

时间:2016-12-14 20:12:14

标签: mysql stored-procedures

当我尝试选择它时,我的存储过程总是返回一个空值。我做了一些研究,我必须指定某处OUTPUT。不知道在哪里或如何做到这一点。

delimiter $$

Create Procedure ClientPurchases (IN idClient INT, outClientAvgPurchases DECIMAL (4,2))
BEGIN 

DECLARE PurchasesAvg DECIMAL(4,2) ;

set PurchasesAvg=
(SELECT AVG(PurchaseAmount) 
FROM Purchase
inner join Client on idClient=Client_idClient);
SET outClientAvgPurchases= PurchasesAvg;

END$$


CALL ClientPurchase(3, @purchase );

SELECT  @purchase as 'Purchase Total';

2 个答案:

答案 0 :(得分:2)

您可以将过程参数声明为INOUTINOUT

Create Procedure ClientPurchases (
    IN idClient INT, 
    OUT outClientAvgPurchases DECIMAL (4,2))
  . . .

http://dev.mysql.com/doc/refman/5.7/en/create-procedure.html

了解详情

答案 1 :(得分:2)

OUT是一个应位于参数名称前面的关键字:

Create Procedure ClientPurchases (IN idClient INT, OUT ClientAvgPurchases DECIMAL (4,2))
...
SET ClientAvgPurchases= PurchasesAvg;