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 
4

1 回答 1

0

正如链接中提到的:

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 应该等到它关闭,无论您是否使用 ' ; ' 或者不是,然后才解释分隔符之间的内容。

于 2010-09-30T14:08:14.860 回答