-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Trigger de INSERT: tia_sga_subcomisiones
-- AFTER INSERT
-- Tabla: sga_subcomisiones
-- Version: 3.0.0
-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-- DROP FUNCTION ftia_sga_subcomisiones ();
CREATE OR REPLACE FUNCTION ftia_sga_subcomisiones () RETURNS trigger AS 
$BODY$
DECLARE
   _cupo smallint;   
BEGIN
   -- Inserto el dato del cupo de la comision. Si es null entonces inserto 0. 
   _cupo := COALESCE(NEW.cupo, 0);
   INSERT INTO sga_subcomisiones_cupo (subcomision, cupo) VALUES (NEW.subcomision, _cupo);

   RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql';


-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Trigger de INSERT sobre sga_subcomisiones
-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- DROP TRIGGER tia_sga_subcomisiones ON sga_subcomisiones RESTRICT;
CREATE TRIGGER tia_sga_subcomisiones
       AFTER INSERT ON sga_subcomisiones 
       FOR EACH ROW EXECUTE PROCEDURE ftia_sga_subcomisiones();

