使用声明的变量返回函数时出错

时间:2019-12-07 21:14:23

标签: mysql sql database

因此,我正在尝试创建一个函数,该函数创建可变的百分比增长,然后使用该值来生成对来年的一个月的预测。 这是我的代码:

CREATE FUNCTION getPredictedSales(forecastmonth int(2))
RETURNS DOUBLE(6,2)
BEGIN
SELECT @percentincrease = (SELECT (SUM(s19.totalsales) / SUM(s18.totalsales))
FROM Sales2018 s18
INNER JOIN Sales2019 s19 ON s18.month = s19.month AND s18.shopname = s19.shopname
WHERE s18.month = forecastmonth)

RETURN (SELECT (SUM(s19.totalsales)*(@percentincrease)) FROM Sales2019 s19 WHERE month = 1));

END

这是错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN (SELECT (SUM(s19.totalsales)*(@percentincrease)) FROM Sales2019 s19 WHERE' at line 9

1 个答案:

答案 0 :(得分:1)

您必须在每个语句的末尾添加分号。 这样您就可以在很大程度上获得回报。

所以应该看起来是这样。

DELIMITER $$
CREATE FUNCTION getPredictedSales(forecastmonth int(2))
RETURNS DOUBLE(6,2)
BEGIN
  SELECT @percentincrease = (SELECT (SUM(s19.totalsales) / SUM(s18.totalsales))
  FROM Sales2018 s18
  INNER JOIN Sales2019 s19 ON s18.month = s19.month AND s18.shopname = s19.shopname
  WHERE s18.month = forecastmonth);

  RETURN (SELECT (SUM(s19.totalsales)*(@percentincrease)) FROM Sales2019 s19 WHERE month = 1);
END $$
DELIMITER ;
相关问题