CREATE OR REPLACE TRIGGER VERIFICA_EXIST_CARD
BEFORE INSERT 
ON CARDADIA FOR EACH ROW
DECLARE
   V_NRCARDAPIO VARCHAR2(10);
BEGIN
   IF :NEW.CDDIETPAD IS NULL THEN
      SELECT MAX(NRCARDAPIO) INTO V_NRCARDAPIO
      FROM CARDADIA
      WHERE CDFILIAL = :NEW.CDFILIAL
        AND CDSERVFILI = :NEW.CDSERVFILI
        AND TRUNC(DTCARDAPIO) = TRUNC(:NEW.DTCARDAPIO)
        AND CDDIETPAD IS NULL;
      IF V_NRCARDAPIO IS NOT NULL THEN
         raise_application_error(-20501, 'Cardapio esta sendo alterado por outro usuario.', True);
      END IF;
   END IF;
END;
/