DECLARE
   V_SEQUENCIAL VARCHAR2(20);
BEGIN
   FOR C_EMP IN (SELECT DISTINCT CDEMPRESA
                   FROM LANCLOTE
                  WHERE CDLOTE = '12') LOOP

      SELECT NVL(MAX(NRLANCLOTE),0)
        INTO V_SEQUENCIAL
        FROM LANCLOTE
       WHERE CDEMPRESA = C_EMP.CDEMPRESA
         AND CDLOTE = '12';

      INSERT INTO NOVOCODIGO
         (CDCONTADOR, NRSEQUENCIAL)
      VALUES
         ('LANCLOTE'||C_EMP.CDEMPRESA||'12', LPAD(V_SEQUENCIAL,20,'0'));
   END LOOP;
END;
/