在 mysql 中创建函数不起作用 - 错误 1064

发布于 2024-09-25 13:06:20 字数 906 浏览 8 评论 0原文

我通过 phpMyAdmin 尝试了这个例子 http://www.databasejournal.com/features /mysql/article.php/3569846/MySQL-Stored-Functions.htm

mysql> DELIMITER |
mysql>
 CREATE FUNCTION WEIGHTED_AVERAGE (n1 INT, n2 INT, n3 INT, n4 INT)
  RETURNS INT
   DETERMINISTIC
    BEGIN
     DECLARE avg INT;
     SET avg = (n1+n2+n3*2+n4*4)/8;
     RETURN avg;
    END|

这有效

DELIMITER |

下一条语句给出:

Error

SQL query:

CREATE FUNCTION WEIGHTED_AVERAGE(
n1 INT,
n2 INT,
n3 INT,
n4 INT
) RETURNS INT DETERMINISTIC BEGIN DECLARE avg INT;

MySQL said: Documentation
#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 '' at line 5 

I tried this example via phpMyAdmin
http://www.databasejournal.com/features/mysql/article.php/3569846/MySQL-Stored-Functions.htm

mysql> DELIMITER |
mysql>
 CREATE FUNCTION WEIGHTED_AVERAGE (n1 INT, n2 INT, n3 INT, n4 INT)
  RETURNS INT
   DETERMINISTIC
    BEGIN
     DECLARE avg INT;
     SET avg = (n1+n2+n3*2+n4*4)/8;
     RETURN avg;
    END|

This worked

DELIMITER |

The next statement gave:

Error

SQL query:

CREATE FUNCTION WEIGHTED_AVERAGE(
n1 INT,
n2 INT,
n3 INT,
n4 INT
) RETURNS INT DETERMINISTIC BEGIN DECLARE avg INT;

MySQL said: Documentation
#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 '' at line 5 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-02 13:06:20

正如链接中提到的:

As mentioned in the first stored procedures tutorial, we declare the "|" symbol as a delimiter, so that our function body can use ordinary ";" characters

您可以在不同的连续行上编写很多命令。但通常只有当满足';'时,才会执行hole语句。
添加 DELIMITER 字符意味着无论您是否使用“;”,MySQL 都应该等到它关闭,然后才解释分隔符之间的内容。

As it is mentioned in the link:

As mentioned in the first stored procedures tutorial, we declare the "|" symbol as a delimiter, so that our function body can use ordinary ";" characters

You can write a lot of commands on different consecutive lines. But usually only when ';' is met, the hole statement is executed.
Putting a DELIMITER character means that MySQL should wait until this is closed no matter if you use ';' or not and only then to interpret what is between delimiters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文