0

I created a trigger that updates the number of species counted from the "effectif" table to the "citation" table. It works well except when I delete the last row, the calculation does not perform and remains at the last state. For example, if I delete the last row on the "effectif" table that represents 6 species, I would still have 6 on my "citation" table where I'm suppose to find 0 or null. Here is my trigger :

CREATE OR REPLACE FUNCTION data.del_eff_tot()
  RETURNS trigger AS
$BODY$
BEGIN
IF OLD.effectif IS NOT NULL THEN
UPDATE data.citation
SET effectif =
(SELECT sum(a.effectif) FROM data.effectif a, data.citation b WHERE OLD.id_cit = a.id_cit AND OLD.id_cit = b.id)+COALESCE((SELECT a.effectif FROM data.effectif a, data.citation b WHERE OLD.id_cit = a.id_cit AND OLD.id_cit = b.id AND sexe = 'sexe_4_1'),0)
WHERE id IN (SELECT id_cit FROM data.effectif a, data.citation b WHERE OLD.id_cit = b.id AND b.id = a.id_cit);
END IF;
RETURN OLD;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION data.del_eff_tot()
  OWNER TO postgres;

------------------------------

CREATE TRIGGER del_eff_tot
  AFTER DELETE
  ON data.effectif
  FOR EACH ROW
  EXECUTE PROCEDURE data.del_eff_tot();
4

0 回答 0