我浏览了一下,其他解决方案似乎不适合我。当尝试在 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
试试这个
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 ;
或者,试试这个,
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 ;
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
我在下面遇到了同样的错误:
错误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 ;