-- ================================================================================
--   Exclui TCOMPPRO
-- ================================================================================
CREATE OR REPLACE TRIGGER Exclui_TCOMPPRO
BEFORE DELETE
ON ESPEPROD FOR EACH ROW
DECLARE
   V_CDTIPOCOMP   TCOMPPRO.CDTIPOCOMP  %TYPE;

BEGIN
   IF :NEW.IDTPMANUAL IS NOT NULL THEN
      SELECT MAX(CDTIPOCOMP)
      INTO V_CDTIPOCOMP
      FROM PARAMCOMIS
      WHERE (CDFILIAL = :NEW.CDFILIAL);

      IF V_CDTIPOCOMP IS NOT NULL THEN
         DELETE FROM TCOMPPRO
         WHERE (CDPRODUTO  = :NEW.CDPRODUTO ) AND
               (CDESPEPROD = :NEW.CDESPEPROD) AND
               (CDTIPOCOMP = V_CDTIPOCOMP   );
      END IF;
   END IF;
END;
/
