CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;
经过数小时的搜索后,首先发布在这里。
我尝试在更新预订表后使用触发器插入book_in_out,我能够执行此操作,但如果我更新bookName
,触发器将尝试运行。
我只希望触发器仅在状态列更改时运行。 status
列的默认值为NULL
以下是我不断得到的错误:
#1064 - 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 5
答案 0 :(得分:1)
尝试手动指定触发器的分隔符,这样就不会与BEGIN / END中使用的分号混淆,即
DELIMITER //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//
答案 1 :(得分:1)
你可以试试这个
delimiter //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (`astatus`, `bid`, `time`) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//
delimiter ;