I have to make a trigger that takes a current_timestamp + minutes and says that if its older than the current_timestamp it should be INSERT INTO a new table.
I have been playing around with this for 8 hours now but can't seem to make it work. Anyone have an solution for this?
This was my idea:
DELIMITER //
CREATE TRIGGER create_comment
AFTER UPDATE ON aflaesning
FOR EACH ROW
BEGIN
IF (TIME_TO_SEC(OLD.forventetSlut) < TIME_TO_SEC(NEW.aktuelSlut-900)) THEN
INSERT INTO aflaesningkommentar(aflaesningaflaesning,aflaesning_id) VALUES('That was too late', NEW.aflaesnings_id);
END IF;
END;
DELIMITER//
It takes the TIMESTAMP (ExpectedEnd) from the table Loadings and have to put the row into the table delayedLoadings if the timestamp is older than minutes. BUT it makes an error on the END IF//
Thanks in advance
The primary problem is the delimiter you are using. You should use a semi-colon after END IF
, and then use the //
after the END
at the end of your trigger (which is also missing).
Also, unrelated to the error, I think you're conditional is wrong. You probably want to do this:
IF (TIME_TO_SEC(OLD.forventetSlut) < TIME_TO_SEC(NEW.aktuelSlut)-900) THEN
Instead of this:
IF (TIME_TO_SEC(OLD.forventetSlut) < TIME_TO_SEC(NEW.aktuelSlut-900)) THEN
So something like this:
DELIMITER //
CREATE TRIGGER create_comment
AFTER UPDATE ON aflaesning
FOR EACH ROW
BEGIN
IF (TIME_TO_SEC(OLD.forventetSlut) < TIME_TO_SEC(NEW.aktuelSlut)-900) THEN
INSERT INTO aflaesningkommentar(aflaesningaflaesning,aflaesning_id) VALUES('That was too late', NEW.aflaesnings_id);
END IF;
END //
DELIMITER;
Try changing your IF condition to:
IF (TIME_TO_SEC(OLD.forventetSlut) - TIME_TO_SEC(NEW.aktuelSlut) < -900) THEN
Also, change the semi-colon after the last END
to //
.
Lastly, try removing all whitespace from your source and replacing with space and newline. Mysql can't handle tab characters in trigger source.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.