ALTER TABLE USR_APT_OPERFORN
MODIFY NRORG NUMBER DEFAULT 1;
/
ALTER TABLE USR_APT_OPERFORN
MODIFY CDOPERADOR VARCHAR2(20) DEFAULT '000000000025'
/
BEGIN
FOR C_FORNECEDOR IN (SELECT CDFORNECED FROM FORNECEDOR WHERE CDFORNECED NOT IN (SELECT CDFORNECED FROM USR_APT_OPERFORN) AND IDTPIJURFORN = 'F') LOOP
    INSERT INTO USR_APT_OPERFORN (CDFORNECED) 
    VALUES (C_FORNECEDOR.CDFORNECED);
   -- DBMS_OUTPUT.PUT_LINE(C_FORNECEDOR.CDFORNECED);
END LOOP;
END;

/

CREATE OR REPLACE TRIGGER INSERT_USR_APT_OPERFORN
  AFTER INSERT OR UPDATE ON FORNECEDOR
  FOR EACH ROW
  DECLARE
      V_CDFORNECED VARCHAR2(20);
      V_IDTPIJURFORN VARCHAR2(1);
BEGIN
  IF INSERTING THEN
      V_CDFORNECED := NVL(:NEW.CDFORNECED, :OLD.CDFORNECED);
      V_IDTPIJURFORN := NVL(:NEW.IDTPIJURFORN, :OLD.IDTPIJURFORN);
      IF(V_IDTPIJURFORN = 'F') THEN
        INSERT INTO USR_APT_OPERFORN (CDFORNECED) VALUES (SUBSTR(V_CDFORNECED, 0, 20));
      END IF;
  END IF;    
END INSERT_USR_APT_OPERFORN;
/
