MySQL 中的 FUNCTION ... 错误中找不到 RETURN

问题描述 投票:0回答:4

我浏览了一下,其他解决方案似乎不适合我。当尝试在 MySQL 中创建此函数时,我不断收到“找不到函数返回”错误。知道为什么吗?

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
mysql database return mysql-function mysql-error
4个回答
1
投票

试试这个

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 ;

1
投票

或者,试试这个,

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 ;

0
投票
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 

0
投票

我在下面遇到了同样的错误:

错误1320(42000):在FUNCTION apple.addition中找不到RETURN

当我尝试创建

addition()
函数时,如下所示:

DELIMITER $$

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

DELIMITER ;

因此,我将 RETURN 语句设置为

addition()
函数,如下所示,然后我可以创建
addition()
函数而不会出现错误:

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 ;
© www.soinside.com 2019 - 2024. All rights reserved.