create or replace TRIGGER ORDENA_COMPCARDPESQ
BEFORE UPDATE OR DELETE
ON CNFCAMES FOR EACH ROW

BEGIN
   FOR C_COM IN (
      SELECT CP.NRCOMPCARDPESQ
        FROM COMPCARDPESQ CP INNER JOIN CARDADIA C ON CP.CDFILIAL = C.CDFILIAL AND C.NRCARDAPIO = CP.NRCARDAPIO
       WHERE C.CDFILIAL = :OLD.CDFILIAL AND C.CDSERVFILI = :OLD.CDSERVFILI AND C.DTMESCARD = :OLD.DTREFECNFMES AND NRSEQPRATCAR = :OLD.NRSEQCNFMES      
   )LOOP
      IF DELETING THEN          
         DELETE FROM COMPCARDPESQ
          WHERE NRCOMPCARDPESQ = C_COM.NRCOMPCARDPESQ;
      ELSE
         UPDATE COMPCARDPESQ CP
            SET NRORDPRATCAR = :NEW.NRORDCNFMES,
                NRORDEM      = :NEW.NRORDCNFMES
          WHERE NRCOMPCARDPESQ = C_COM.NRCOMPCARDPESQ;
      END IF;
   END LOOP;    
END;