tags:

views:

209

answers:

2

Herewith I have given the stored procedure "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"?

A: 

Hi,

I'm no mysql expert, but do you not need to refer to the OUT variable at any point and assign it a value?

For example as seen on 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'

Philip
A: 

you are not setting the OUT parameter in your select.

try updating your statement to:

SET @userVar = CONCAT('SELECT AVG( ' , col , ' ) INTO average FROM ' , tbl );
Josh
Thanks for answering. When I did it, "Undeclared variable: average" error is thrown by MySQL. But I have alread declared average while creating procedureCREATE DEFINER = 'MailIntimator'@'127.0.0.1' PROCEDURE GetAvgOut(OUT average INT,IN col VARCHAR(30),IN tbl VARCHAR(30))
jezhilvalan