在此我给出了存储过程“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 ;
我尝试使用
调用上述程序CALL GetAvgOut(@a,'Population','city');
SELECT @a;
“select @a”返回null。如何获得分配给参数“@a”的平均值?
答案 0 :(得分:1)
我不是mysql专家,但你不需要在任何时候引用OUT变量并给它赋值吗?
例如,见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
菲尔'
答案 1 :(得分:1)
您没有在选择中设置OUT参数。
尝试将您的陈述更新为:
SET @userVar = CONCAT('SELECT AVG( ' , col , ' ) INTO average FROM ' , tbl );
答案 2 :(得分:0)
正如@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