MySQL存储函数创建错误ERROR 1064& 1327

时间:2010-08-04 15:00:33

标签: sql mysql function mysql-error-1064 mysql-error-1327

我正在使用MySQL v5.1.36,我正在尝试使用此代码创建存储函数。

DELIMITER //
CREATE FUNCTION `modx`.getSTID (x VARCHAR(255)) RETURNS INT DETERMINISTIC
    BEGIN
    DECLARE y INT;
    SELECT id INTO y
    FROM `modx`.coverage_state
    WHERE `coverage_state`.name = x;
    RETURN y;
    END//

当进入MySQL控制台时,我得到了这个回复。

mysql>  DELIMITER //
mysql>  CREATE FUNCTION `modx`.getSTID (x VARCHAR(255)) RETURNS INT DETERMINISTIC
    ->          BEGIN
    ->          DECLARE y INT;
ERROR 1064 (42000): 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 3
mysql>          SELECT id INTO y
    ->          FROM `modx`.coverage_state
    ->          WHERE `coverage_state`.name = x;
ERROR 1327 (42000): Undeclared variable: y
mysql>          RETURN y;
ERROR 1064 (42000): 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 'RETUR
N y' at line 1
mysql>          END//

从我在网上找到的我的语法是正确的。我做错了什么?

2 个答案:

答案 0 :(得分:7)

从mysql控制台创建函数/过程时,第一个命令应为DELIMITER //。否则,它使用默认分隔符(;),

答案 1 :(得分:6)

我通过在我的变量周围添加`来解决我的问题。这是我最终得到的代码。

DELIMITER //
CREATE FUNCTION `modx`.getSTID (x VARCHAR(255)) RETURNS INT DETERMINISTIC
    BEGIN
    DECLARE `y` INT;
    SELECT id INTO `y`
    FROM `modx`.coverage_state
    WHERE `coverage_state`.name = `x`;
    RETURN `y`;
    END//
DELIMITER ;