Perform condition on Trigger

Asked

Viewed 503 times

0

I have a Rigger that updates my table. I wanted to know how to make Trigger only run if the status column is different from the one already in my table, if it is the same 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 ;

thanks in advance.

1 answer

0


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

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 ;
  • Corrected my mistake here thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.