No RETURN found in FUNCTION ... error in MySQL

7k Views Asked by At

I have browsed around and other solutions don't seem to be working for me here. I keep getting a 'No return found for function' error when trying to create this function in MySQL. Any idea why?

CREATE FUNCTION `mydb`.`myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
SELECT SUM(Transaction.Bought) INTO @Qty FROM Transaction WHERE Transaction.Name = Name;
RETURN @Qty
4

There are 4 best solutions below

1
On BEST ANSWER

Try this

DELIMITER $$
CREATE FUNCTION `myfunction`(`Name` VARCHAR(20) CHARSET utf8) RETURNS INT NOT DETERMINISTIC
READS SQL DATA
MAIN: BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal FROM `Transaction` WHERE `Transaction`.Name = Name;
RETURN returnVal;
END MAIN;$$
DELIMITER ;
0
On

I got the same error below:

ERROR 1320 (42000): No RETURN found in FUNCTION apple.addition

When I tried to create addition() function without RETURN statement as shown below:

DELIMITER $$

CREATE FUNCTION addition(value INT) RETURNS INT 
DETERMINISTIC
BEGIN
UPDATE test SET num = num + value;
SELECT num INTO value FROM test;
END$$

DELIMITER ;

So, I set RETURN statement to addition() function as shown below, then I could create addition() function without error:

DELIMITER $$

CREATE FUNCTION addition(value INT) RETURNS INT 
DETERMINISTIC
BEGIN
UPDATE test SET num = num + value;
SELECT num INTO value FROM test;
RETURN value; -- Here
END$$

DELIMITER ;
0
On
delimiter //
create function myfun1234 (i int,j int)
returns int 
begin 
      declare x int ;
       declare    y int ;
        declare  z int ;
      set x=10;
      set y=20;
      if (1<=x && j>=y )then 
      set z=i+j;
      end if;
    return z;
      end; //


-- error1   FUNCTION myfun12 ended without RETURN 
4
On

Alternatively, try this,

DELIMITER $$
CREATE FUNCTION `myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
BEGIN

  DECLARE returnVal int;

  SELECT SUM(`Transaction`.Bought) INTO returnVal 
  FROM `Transaction` 
  WHERE `Transaction`.Name = Name;

  RETURN returnVal;

END$$
DELIMITER ;