我正在使用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//
从网上可以找到我的语法是正确的。我究竟做错了什么?
我通过在变量周围添加``解决了我的问题。这是我最终得到的代码。
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 ;