Trigger in mysql causing error

爷,独闯天下 提交于 2019-12-20 04:22:15

问题


What is the error in the following code. I am executing in mysql

CREATE TRIGGER tg_order_insert
BEFORE INSERT 
    ON `order` FOR EACH ROW
BEGIN
    INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
    SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END;

Grocery is the database and order_seqid and order are 2 table. order_seqid is a table with only 1 attribute if type int and auto increment.
Am trying to put a prefix on the id which we insert into order table. I am getting 2 errors in INSERT INTO..... and END; line


回答1:


Did you declare a delimiter before your trigger definition? Something like

DELIMITER //
CREATE TRIGGER tg_order_insert
BEFORE INSERT 
    ON `order` FOR EACH ROW
BEGIN
    INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
    SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END
//

Because if you don't, then MySQL thinks you're trying to end your trigger definition when it sees that first ; and calls syntax error.



来源:https://stackoverflow.com/questions/43104422/trigger-in-mysql-causing-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!