MySQL触发器不保存

时间:2014-06-29 07:23:29

标签: mysql sql

执行此操作:

CREATE TRIGGER `after_order_insert`
    AFTER INSERT ON `hb_orders` FOR EACH ROW
    BEGIN
       UPDATE hb_accounts
       SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
                     WHERE hb_client_details.id = NEW.client_id
                            LIMIT 1)
       WHERE hb_accounts.client_id = NEW.client_id;
    END

结果如下:

/ * SQL错误(1064):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第8行* /

附近使用正确的语法

我错过了什么?这应该有用,不应该吗?

由于

2 个答案:

答案 0 :(得分:2)

您最有可能尝试在不更改分隔符的情况下添加触发器。由于触发器包含分号,因此您必须暂时将分隔符更改为其他内容;

DELIMITER //

CREATE TRIGGER `after_order_insert`
    AFTER INSERT ON `hb_orders` FOR EACH ROW
    BEGIN
       UPDATE hb_accounts
       SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
                     WHERE hb_client_details.id = NEW.client_id
                            LIMIT 1)
       WHERE hb_accounts.client_id = NEW.client_id;
    END //

DELIMITER ;

An SQLfiddle with the trigger adding successfully。请注意,分隔符在设置中更改

答案 1 :(得分:0)

这是另一个实现:

CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;