简体   繁体   English

带OUT参数的MySQL存储过程

[英]MySQL stored procedure with OUT parameter

I keep getting a null value when using an out parameter. 使用out参数时,我一直得到null值。

CREATE PROCEDURE `getTotalPoints`(IN `uEmail` VARCHAR(255), OUT `iTotal` DECIMAL(5,2) UNSIGNED)
begin
select
coalesce(
coalesce(sum(points_earned),0) -
coalesce(sum(points_lost),0) +
coalesce(sum(points_taken),0) -
coalesce(sum(points_traded),0),0)
from user_activities
where user_email=@uEmail into @iTotal;
end

I know the select statement works fine as without the out parameter, I get 0.00 as a result. 我知道select语句可以正常工作,因为没有out参数,结果是0.00。

I'm sure it's something simple, but everything I've researched and tried has resulted in the same NULL return value. 我敢肯定这很简单,但是我研究和尝试的一切都导致了相同的NULL返回值。

Remove the " @ " symbols on the variable references. 删除变量引用上的“ @ ”符号。

" @uEmail " and " @iTotal " are references to user-defined variables, not the procedure arguments. @uEmail ”和“ @iTotal ”是对用户定义的变量的引用,而不是过程参数。

You want to reference the procedure arguments as " uEmail " and " iTotal ", without the " @ ". 您想将过程参数引用为“ uEmail ”和“ iTotal ”,而不使用“ @ ”。


Or , you could probably get your statement to work if you did the necessary assignments to and from the user-defined variables, something like this in the body of the procedure: 或者 ,如果您对用户定义的变量进行了必要的赋值,则可能使您的语句生效,在过程主体中类似以下内容:

-- set user-defined variable to value from procedure argument
SET @uEmail = uEmail;

-- statement references user-defined variables
SELECT ...@uEmail ... INTO @iTotal;

-- set procedure OUT argument from user-defined variable
SET iTotal = @iTotal;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM