Herewith I have given the stored procedure "GetAvgOut":
在此我给出了存储过程“GetAvgOut”:
delimiter //
DROP PROCEDURE IF EXISTS GetAvgOut//
CREATE DEFINER = 'MailIntimator'@'127.0.0.1' PROCEDURE GetAvgOut(OUT average INT,IN col VARCHAR(30),IN tbl VARCHAR(30))
READS SQL DATA
COMMENT 'returns average'
BEGIN
SET @userVar = CONCAT(' SELECT AVG( ' , col , ' ) FROM ' , tbl );
PREPARE stmt FROM @userVar;
EXECUTE stmt;
END;
//
delimiter ;
I tried calling the aforeseen procedure using,
我尝试使用上述程序调用,
CALL GetAvgOut(@a,'Population','city');
SELECT @a;
"select @a" returns null. How can I get the average which is assigned to out parameter "@a"?
“select @a”返回null。如何获得分配给参数“@a”的平均值?
3 个解决方案
#1
I'm no mysql expert, but do you not need to refer to the OUT variable at any point and assign it a value?
我不是mysql专家,但你不需要在任何时候引用OUT变量并为其赋值吗?
For example as seen on http://dev.mysql.com/doc/refman/5.0/en/call.html:
例如,如http://dev.mysql.com/doc/refman/5.0/en/call.html所示:
CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
# Set value of OUT parameter
SELECT VERSION() INTO ver_param;
# Increment value of INOUT parameter
SET incr_param = incr_param + 1;
END;
HTH
Phil'
#2
you are not setting the OUT parameter in your select.
您没有在选择中设置OUT参数。
try updating your statement to:
尝试将您的声明更新为:
SET @userVar = CONCAT('SELECT AVG( ' , col , ' ) INTO average FROM ' , tbl );
#3
As @Philip mentioned, you need to set the value of the OUT variable. Combining @Josh's and @Philip's answer gives something like this
正如@Philip所提到的,您需要设置OUT变量的值。结合@Josh和@ Philip的答案给出了这样的结论
BEGIN
SET @userVar = CONCAT(' SELECT AVG( ' , col , ' ) into @average FROM ' , tbl , ' ');
PREPARE stmt FROM @userVar;
EXECUTE stmt;
set average = @average;
END
#1
I'm no mysql expert, but do you not need to refer to the OUT variable at any point and assign it a value?
我不是mysql专家,但你不需要在任何时候引用OUT变量并为其赋值吗?
For example as seen on http://dev.mysql.com/doc/refman/5.0/en/call.html:
例如,如http://dev.mysql.com/doc/refman/5.0/en/call.html所示:
CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
# Set value of OUT parameter
SELECT VERSION() INTO ver_param;
# Increment value of INOUT parameter
SET incr_param = incr_param + 1;
END;
HTH
Phil'
#2
you are not setting the OUT parameter in your select.
您没有在选择中设置OUT参数。
try updating your statement to:
尝试将您的声明更新为:
SET @userVar = CONCAT('SELECT AVG( ' , col , ' ) INTO average FROM ' , tbl );
#3
As @Philip mentioned, you need to set the value of the OUT variable. Combining @Josh's and @Philip's answer gives something like this
正如@Philip所提到的,您需要设置OUT变量的值。结合@Josh和@ Philip的答案给出了这样的结论
BEGIN
SET @userVar = CONCAT(' SELECT AVG( ' , col , ' ) into @average FROM ' , tbl , ' ');
PREPARE stmt FROM @userVar;
EXECUTE stmt;
set average = @average;
END