Perform trigger condition

0

I have a trigger that does an update on my table. I would like to know how to do so that the trigger is only executed if the status column is different from what is already in my table, if it does not, nothing happens.

DELIMITER $$
CREATE TRIGGER trg_anuencias
AFTER UPDATE ON anuencias
FOR EACH ROW BEGIN
INSERT INTO anuencias_auditoria
SET acao = 'update',
id_anuencia = NEW.id,
status = NEW.status,
modificadoem = NOW(); END$$
DELIMITER ;

Thank you in advance.

    
asked by anonymous 13.07.2017 / 20:22

1 answer

0

NEW takes the new values and OLD takes the old values, follow the example below:

DELIMITER $$
CREATE TRIGGER trg_anuencias
AFTER UPDATE ON anuencias
FOR EACH ROW BEGIN
    IF (NEW.status <> OLD.status) THEN
        INSERT INTO anuencias_auditoria
        SET acao = 'update', id_anuencia = NEW.id, status = NEW.status,
        modificadoem = NOW();
    END IF;
END$$
DELIMITER ;
    
13.07.2017 / 20:27