在mysql中创建函数不起作用-错误1064 [英] Creating functions in mysql doesnt work - Error 1064

查看:1004
本文介绍了在mysql中创建函数不起作用-错误1064的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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|

这有效

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 

推荐答案

在链接中已提到:

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都应等到关闭为止,然后才解释分隔符之间的含义.

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.

这篇关于在mysql中创建函数不起作用-错误1064的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆