CREATE OR REPLACE TRIGGER DELETA_CONTCTBLMAPA
BEFORE DELETE
ON LANCCONT FOR EACH ROW
DECLARE
   V_MAPABLOQ VARCHAR2(01); 
BEGIN

   V_MAPABLOQ := 'S';

   BEGIN
      SELECT NVL(M.IDBLOQMAPAPU,'N')
        INTO V_MAPABLOQ
        FROM MAPAAPURAC M, CONTCTBLMAPA C
      WHERE C.CDEMPRESA   = :OLD.CDEMPRESA
        AND C.NRLANCCONT  = :OLD.NRLANCCONT
        AND C.NRSEQLANC   = :OLD.NRSEQLANC
        AND M.NRSEQUMAPAPU = C.NRSEQUMAPAPU;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_MAPABLOQ := 'N';
   END;

   IF (NVL(V_MAPABLOQ,'N') <> 'S') AND (V_MAPABLOQ IS NOT NULL) THEN
      DELETE FROM CONTCTBLMAPA
       WHERE CDEMPRESA   = :OLD.CDEMPRESA
         AND NRLANCCONT  = :OLD.NRLANCCONT
         AND NRSEQLANC   = :OLD.NRSEQLANC;
   END IF;
END;
/
