Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

sql - Trigger in mysql causing error

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...