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

发布于 2024-09-13 01:19:01 字数 1140 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-09-20 01:19:01

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

When creating a function/procedure from mysql console, the first command should be DELIMITER //. Otherwise, it uses default delimiter (;),

梦年海沫深 2024-09-20 01:19:01

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

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 ;

I solved the my issues by adding `` around my variables. Here is the code I ended up with.

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 ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文