mysql 触发器查询语法错误

发布于 2024-12-10 12:47:57 字数 297 浏览 1 评论 0原文

所以我有以下查询:

CREATE TRIGGER `before_delete`
    BEFORE DELETE ON `abc` FOR EACH ROW
    BEGIN
        DELETE FROM def WHERE OLD.id = objID1 OR OLD.id = objID2;
        DELETE FROM ghi WHERE OLD.id = objID;
    END;

但是mysql抱怨第1行'END'附近有语法错误....

我做错了什么?

so I have the following query:

CREATE TRIGGER `before_delete`
    BEFORE DELETE ON `abc` FOR EACH ROW
    BEGIN
        DELETE FROM def WHERE OLD.id = objID1 OR OLD.id = objID2;
        DELETE FROM ghi WHERE OLD.id = objID;
    END;

but then mysql complains that there's syntax error near 'END' at line 1....

what did I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烟花肆意 2024-12-17 12:47:57

您需要使用 DELIMTIER 技巧在过程块中包含多个语句。否则它无法区分过程的结束和其中语句的结束。在 文档 页面上,有一个使用分隔符关键字。

为了省去您转到该页面并四处查看的麻烦,我认为这可以解决它:

DELIMITER $
CREATE TRIGGER `before_delete`
BEFORE DELETE ON `abc` FOR EACH ROW
BEGIN
    DELETE FROM def WHERE OLD.id = objID1 OR OLD.id = objID2;
    DELETE FROM ghi WHERE OLD.id = objID;
END $
DELIMITER ;

You need to make use of the DELIMTIER trick to include multiple statements in a procedure block. Otherwise it can't tell the difference between the end of your procedure and the end of statements inside it. On the documentation page, there is an example using the delimiter keyword.

To save you the trouble of going to that page and looking around, I think this will fix it:

DELIMITER $
CREATE TRIGGER `before_delete`
BEFORE DELETE ON `abc` FOR EACH ROW
BEGIN
    DELETE FROM def WHERE OLD.id = objID1 OR OLD.id = objID2;
    DELETE FROM ghi WHERE OLD.id = objID;
END $
DELIMITER ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文