CREATE OR REPLACE TRIGGER COMPL_PROD_ITEMCOLE
BEFORE INSERT
ON ITEMCOLE FOR EACH ROW
DECLARE
   V_DSOBSITMCOL     ITEMCOLE.DSOBSITMCOL%TYPE;
   V_IDAPROVITMSOLGR PARASUPRI.IDAPROVITMSOLGR%TYPE;
BEGIN
   SELECT NVL(IDAPROVITMSOLGR, 'N')
     INTO V_IDAPROVITMSOLGR
     FROM PARASUPRI
    WHERE CDFILIAL = :NEW.CDFILIAL;
   IF V_IDAPROVITMSOLGR = 'S' THEN
      SELECT SUBSTR(MAX(TXOBSITMSOL), 1, 1000) INTO V_DSOBSITMCOL
        FROM ITEMSOLI
       WHERE CDFILSOLI IN (SELECT CDFILIAL FROM FILIAL WHERE CDREGIAO = :NEW.CDREGIAO)
         AND CDPRODUTO = :NEW.CDPRODUTO
         AND DTPROGENTR BETWEEN :NEW.DTINIVALITM AND :NEW.DTFINVALITM
         AND TXOBSITMSOL IS NOT NULL;
      IF V_DSOBSITMCOL IS NOT NULL THEN
         :NEW.DSOBSITMCOL := V_DSOBSITMCOL;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('COMPL_PROD_ITEMCOLE','5.40.xxx'); END;
/
