Creating functions in mysql doesnt work - Error 1064

给你一囗甜甜゛ 提交于 2019-12-25 11:58:08

问题


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 

回答1:


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.



来源:https://stackoverflow.com/questions/3831077/creating-functions-in-mysql-doesnt-work-error-1064

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!