Trigger para update before Insert

Asked

Viewed 1,010 times

4

My database has two tables, notification and attendance, as shown in the image.

Tabelas do BD

In the notification table the default value of the status is "open".

I tried to make a Rigger that updates the default status "open" to "in attendance" after a new attendance table is inserted My Rigger ended up changing the status of all records in the notification table, and I want to change only the status of the notification that is referenced in the attendance table.

Below is the code of my Rigger

DELIMITER //
CREATE TRIGGER atualizanotificacao BEFORE INSERT ON atendimento
FOR EACH ROW
BEGIN
    UPDATE notificacao SET status = "Em atendimento" WHERE idnotificacao = idnotificacao;

END
// DELIMITER ;

1 answer

4


It is changing all records because idnotification = idnotified is the same as 1 = 1. in all rows this condition would return true.

DELIMITER //
CREATE TRIGGER atualizanotificacao BEFORE INSERT ON atendimento
FOR EACH ROW
BEGIN
    UPDATE notificacao SET status = "Em atendimento" WHERE idnotificacao = NEW.idnotificacao;

END
// DELIMITER ;

This NEW which I added means that I will take the value of the notification that will be entered in the table meeting, or notification without the NEW is of the same notification table.

  • That’s right! Thank you very much :)

Browser other questions tagged

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