DECLARE 
  type vEstados is varray(27) of varchar2(2);
  estados vEstados;
  nomSeq VARCHAR2(200);
  qtdeTotal INTEGER;
  object_exists exception;
  pragma exception_init (object_exists , -00955);
BEGIN 
  estados := vEstados('AC', 'AL', 'AM', 'AP', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MG', 'MS', 'MT', 'PA', 'PB', 'PE', 'PI', 'PR', 'RJ', 'RN', 'RO', 'RR', 'RS', 'SC', 'SE', 'SP', 'TO');
  qtdeTotal := estados.count;
  FOR EMP IN (SELECT EMPRESA_ID FROM EMPRESA WHERE ACTIVO = 1 AND EMPRESA_ID > 0)
  LOOP
    FOR i in 1 .. qtdeTotal LOOP
      BEGIN
        SELECT SEQUENCE_NAME INTO nomSeq from DBA_SEQUENCES where SEQUENCE_owner = (select user from dual) AND SEQUENCE_NAME like 'FOLIO_SISTEMA_BPE_' || estados(i) || '_' || EMP.EMPRESA_ID || '_SEQ%';
      EXCEPTION WHEN NO_DATA_FOUND THEN
        EXECUTE IMMEDIATE 'CREATE SEQUENCE ' || 'FOLIO_SISTEMA_BPE_' || estados(i) || '_' || EMP.EMPRESA_ID || '_SEQ MINVALUE 1 MAXVALUE 999999999 INCREMENT BY 1 START WITH 1 ORDER NOCACHE CYCLE';
      END;
    END LOOP;
  END LOOP;
  EXCEPTION WHEN object_exists THEN NULL;
END;

