OlZ
OlZ

Reputation: 346

How to insert data with a trigger if some data are note the same

I hoped something as below will be successful. But SQL syntax error... Clearly i don't want to insert data into a table if only a special data is changed.

CREATE TRIGGER `before_access_update` BEFORE UPDATE ON `access` FOR EACH ROW IF NOT (NEW.pin<>OLD.pin) THEN
INSERT INTO access_audit
SET action = 'update',
    a_lastname = OLD.lastname,
    a_firstname = OLD.firstname,
    changed_on = NOW();
END IF;

Upvotes: 0

Views: 23

Answers (1)

Jean-Karim Bockstael
Jean-Karim Bockstael

Reputation: 1405

You use a ; in your trigger, therefore you have to use another query delimiter for the trigger itself:

DELIMITER $$
CREATE TRIGGER `before_access_update` BEFORE UPDATE ON `access` FOR EACH ROW IF NOT (NEW.pin<>OLD.pin) THEN
INSERT INTO access_audit
SET action = 'update',
    a_lastname = OLD.lastname,
    a_firstname = OLD.firstname,
    changed_on = NOW();
END IF$$
DELIMITER ;

Upvotes: 1

Related Questions