create or replace TRIGGER INSERE_EMBALAGEM_CNFCAMES
BEFORE INSERT
ON CNFCAMES FOR EACH ROW

DECLARE
  
   V_CDCONTAINER VARCHAR2(5);
   V_NRGRUPO NUMBER;
BEGIN
   /*Verifica se o servico ja existe para a agregada*/
   IF :NEW.CDCONTAINER IS NULL THEN
   
       SELECT MIN(CDCNFDISTEMBSERV), MIN(NRGRUPO)
         INTO V_CDCONTAINER, V_NRGRUPO
         FROM CNFDISTEMBSERV
        WHERE CDFILIAL = :NEW.CDFILIAL AND
              CDSERVFILI = :NEW.CDSERVFILI AND
              NRSEQCNFCARD = :NEW.NRSEQCNFMES AND
              CDCLIENTE IS NULL;
        IF V_CDCONTAINER IS NOT NULL THEN
           :NEW.CDCONTAINER := V_CDCONTAINER;
           :NEW.NRGRUPOEMB := V_NRGRUPO;
        END IF;
    END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('INSERE_EMBALAGEM_CNFCAMES','6.05.000'); END;
/
