我正在使用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//
从我在网上找到的我的语法是正确的。我做错了什么?
答案 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 ;