ALTER TABLE PARAMEMPREINF MODIFY CDVERSAO VARCHAR2(10)
/

DECLARE 
   V_COUNT  NOVOCODIGO.NRSEQUENCIAL%TYPE;
   V_NRSEQ  EVENTOREINF.NREVENTO%TYPE;
BEGIN
   V_COUNT := NULL;
   BEGIN
      SELECT NRSEQUENCIAL
        INTO V_COUNT
        FROM NOVOCODIGO
       WHERE CDCONTADOR = 'EVENTOREINF'; 
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_COUNT := NULL;
   END;

   IF V_COUNT IS NULL THEN
      BEGIN
         SELECT MAX(NREVENTO)
           INTO V_NRSEQ
           FROM EVENTOREINF; 
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQ := NULL;
      END;
      IF V_NRSEQ IS NULL THEN
         INSERT INTO NOVOCODIGO
            (CDCONTADOR, NRSEQUENCIAL)
         VALUES
            ('EVENTOREINF',LPAD(NVL(V_COUNT,0),20,'0'));
      END IF;
   END IF;
END;
/

DECLARE 
   V_COUNT NOVOCODIGO.NRSEQUENCIAL%TYPE;
BEGIN
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-1000 - Informacoes do Contribuinte','R-1000','I','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-1000'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-1070 - Tabela de processos administrativos/judiciais','R-1070','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-1070'
                            AND DSVERSAO = '1.05.01'));                            
   
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2010 - Retencao de contribuicao previdenciaria - servicos tomados','R-2010','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2010'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2020 - Retencao de contribuicao previdenciaria - servicos prestados','R-2020','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2020'
                            AND DSVERSAO = '1.05.01'));                            

   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2030 - Recursos recebidos por associacao desportiva','R-2030','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2030'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2040 - Recursos repassados para associacao desportiva','R-2040','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2040'
                            AND DSVERSAO = '1.05.01'));                            
   
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2050 - Comercializacao da producao por produtor rural PJ/agroindustria','R-2050','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2050'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2055 - Aquisicao de producao rural','R-2055','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2055'
                            AND DSVERSAO = '1.05.01'));                            

NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2060 - Contribuicao previdenciaria sobre a receita bruta - CPRB','R-2060','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2060'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2098 - Reabertura dos eventos periodicos','R-2098','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2098'
                            AND DSVERSAO = '1.05.01'));                            
   
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-2099 - Fechamento dos eventos periodicos','R-2099','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-2099'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-3010 - Receita de espetaculos desportivos','R-3010','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-3010'
                            AND DSVERSAO = '1.05.01'));                            

NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-5001 - Informacoes de bases e tributos por evento','R-5001','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-5001'
                            AND DSVERSAO = '1.05.01'));
                            
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-5011 - Informacoes de bases e tributos consolidadas por periodo de apuracao','R-5011','P','1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-5011'
                            AND DSVERSAO = '1.05.01'));                            
   
   NOVO_CODIGO('EVENTOREINF',V_COUNT);
   INSERT INTO EVENTOREINF
      (NREVENTO, DSEVENTO, NMEVENTO, IDGRUPO, DSVERSAO)
      (SELECT V_COUNT, 'R-9000 - Exclusao de eventos','R-9000',null,'1.05.01'
        FROM DUAL
       WHERE NOT EXISTS (SELECT * FROM EVENTOREINF
                          WHERE NMEVENTO = 'R-9000'
                            AND DSVERSAO = '1.05.01'));							

   COMMIT;
END;    
/