2014-05-20 54 views
1
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; 

第一篇文章在這裏經過幾個小時的搜索。當我使用IS NOT NULL時,無法創建MySQL TRIGGER

我試圖插入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

觸發升好的。唯一缺少的是分隔符。你什麼時候遇到錯誤?寫完觸發器或更新表並觸發器被執行後? –

+0

它看起來對我來說沒關係... http://sqlfiddle.com/#!2/10a266/1你能重現這個問題嗎? – fthiella

+0

感謝您的快速反應,我真的很感激它。無論錯誤如何,我都會創建觸發器(我在觸發器表中看到它)。我正在使用phpMyAdmin 4.0。它忽略了OLD.status爲NULL時的觸發器,但現在的問題是,如果不是NULL,並且NEW.status不是NULL和NEW.status!= OLD.status,那麼Insert將不起作用。 – vinsentos

回答

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 ; 
+0

謝謝KielSoft,'DELIMITER // CREATE TRIGGER c_attend AFTER UPDATE對兒童 FOR EACH ROW BEGIN IF(!OLD.status IS NOT NULL和NEW.status IS NOT NULL和NEW.status = OLD.status) THEN INSERT INTO children_attendance(cid,astatus,time)VALUES(OLD.id,NEW.status,NOW()); END IF; END;'這個工作沒有錯誤,但插入觸發器不起作用。 – vinsentos

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;// 
+0

謝謝,在開始處添加DELIMITER //使其起作用 – vinsentos

+0

現在的問題是如果條件滿足,Insert將不起作用。請幫助。謝謝 – vinsentos

+0

您是否將分隔符設置回;帶分隔符;命令?你現在的錯誤是什麼? – Niemand