Alright, I am trying to create a trigger in MySQL that will insert the old date in the table into another table to track the changes that occur. The following tables is what I have. I cannot change the users table but I can change the user_changes table. I created this trigger below but it keeps giving me an error on the line with the insert statement.
users: id, StoreNbr, UserNbr, LvlNbr, Paid1, Paid2 user_changes: id, store_nbr, user_number, level_number, OldPaid1, OldPaid2, create
DROP TRIGGER IF EXISTS usersToChanges;
CREATE TRIGGER `usersToChanges` AFTER UPDATE ON users FOR EACH ROW
BEGIN
IF LENGTH(CONV(NEW.Status, 10, 2)) NOT IN (5,6,8) AND (NOT(NEW.Paid1 <=> OLD.Paid1) OR NOT(NEW.Paid2 <=> OLD.Paid2))
THEN
INSERT INTO user_changes (store_nbr, user_number, level_number, OldPaid1, OldPaid2, created)
VALUES (OLD.StoreNbr, OLD.UserNbr, OLD.LvlNbr, OLD.Paid1, OLD.Paid2, NOW());
END IF;
END;
You need to use the DELIMTER command to define procedural code:
DROP TRIGGER IF EXISTS usersToChanges;
DELIMITER $$
CREATE TRIGGER `usersToChanges` AFTER UPDATE ON users FOR EACH ROW
BEGIN
IF LENGTH(CONV(NEW.Status, 10, 2)) NOT IN (5,6,8) AND (NOT(NEW.Paid1 <=> OLD.Paid1) OR NOT(NEW.Paid2 <=> OLD.Paid2))
THEN
INSERT INTO user_changes (store_nbr, user_number, level_number, OldPaid1, OldPaid2, created)
VALUES (OLD.StoreNbr, OLD.UserNbr, OLD.LvlNbr, OLD.Paid1, OLD.Paid2, NOW());
END IF;
END $$
DELIMITER ;
The semicolon usually delimits a single command, not procedural code. Notice how I changed the END;
to END $$
to finish making the trigger. Then, I switched back to the default delimiter of semicolon.
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.