Is there a parameter passing in a trigger?

5

I have two tables, one with the sessions and another with the activities. Within the activities table I have two fields, available and salable and within session I have vacancies.

I would like to get the id of the session to be able to do the update in the salable field, when zerosse the vacancies that the activity.vendible field did update to 0.

DELIMITER $$
CREATE TRIGGER atualizarStatus
AFTER UPDATE ON sessao
FOR EACH ROW
BEGIN
IF (sessao.vagas = 0) THEN
UPDATE atividade SET vendivel = 0 WHERE id = $id;
END IF;
END;
    
asked by anonymous 23.07.2015 / 20:33

1 answer

1

Your attempt was very close to achieving the goal. Try this:

DELIMITER $$
CREATE TRIGGER atualizarStatus
   AFTER UPDATE ON sessao 
     FOR EACH ROW
   BEGIN
   IF NEW.vagas = 0 THEN
      UPDATE atividade SET vendivel = 0 WHERE id = NEW.id;
   END IF;
END;

In the trigger, the reserved words NEW and OLD allow access to the columns of records that were affected by the statement that triggered the trigger. In this case you can use NEW to access the status of the registry after the UPDATE.

    
23.07.2015 / 22:18