mysqldatabasereturnmysql-functionmysql-error

No RETURN found in FUNCTION ... error in MySQL


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

Solution

  • 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 ;