MySQL 存储函数创建错误 ERROR 1064 &第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//
从我在网上可以找到的内容来看,我的语法是正确的。我做错了什么?
I am using MySQL v5.1.36 and I am trying to create a stored function using this code.
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//
When entered into the MySQL Console I get this response.
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//
From what I can find online my syntax is correct. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 mysql 控制台创建函数/过程时,第一个命令应该是
DELIMITER //
。否则,它使用默认分隔符(;
),When creating a function/procedure from mysql console, the first command should be
DELIMITER //
. Otherwise, it uses default delimiter (;
),我通过在变量周围添加 `` 解决了我的问题。这是我最终得到的代码。
I solved the my issues by adding `` around my variables. Here is the code I ended up with.