[Oracle] - Procedimento para resetar  sequences
Autor: Emerson S. Gaudencio | Publicado: 2016-07-13T15:31:00-04:00




Bom Pessoal,
&nbsp;Essa é mais uma Postagem para ajudar em problemas do dia-a-dia, caso faça alguma restauração e os valores das "sequences" do banco estejam desatualizados, esse procedimento corrige a sequencia certa as "sequences" com base nos valores das tabelas informadas na base de dados. Segue abaixo, procedimento para efetuar está manutenção:



declare
&nbsp; v_owner &nbsp; &nbsp; &nbsp; VARCHAR2(32) := 'SISTESTE';
&nbsp; v_nome_tabela VARCHAR2(32);
&nbsp; v_maxID &nbsp; &nbsp; &nbsp; NUMBER(18);
&nbsp; v_sql_max &nbsp; &nbsp; varchar2(30);
&nbsp; v_sql_seq_c &nbsp; VARCHAR2(500);
&nbsp; v_sql_seq_d &nbsp; VARCHAR2(500);
&nbsp; v_sql_seq_m &nbsp; VARCHAR2(500);
&nbsp; v_sql_trg &nbsp; &nbsp; VARCHAR2(500);
&nbsp; v_erro &nbsp; &nbsp; &nbsp; &nbsp;VARCHAR2(500);
&nbsp; CURSOR C_SEQUENCE IS
&nbsp; &nbsp; SELECT TRIM(T.sequence_name) as nome_sequence, T.*
&nbsp; &nbsp; &nbsp; FROM ALL_SEQUENCES T
&nbsp; &nbsp; &nbsp;WHERE T.sequence_owner = v_owner
&nbsp; &nbsp; -- &nbsp;AND T.SEQUENCE_NAME = 'IDTBCIDADE'
&nbsp; &nbsp; &nbsp;ORDER BY TRIM(T.sequence_name);
&nbsp; -------------------------------
&nbsp; CURSOR C_TABELA(cv_owner IN VARCHAR2, cv_indice IN VARCHAR2) IS
&nbsp; &nbsp; SELECT unique T0.table_name AS tabela
&nbsp; &nbsp; &nbsp; FROM all_CONS_COLUMNS T0
&nbsp; &nbsp; &nbsp;INNER JOIN all_CONSTRAINTS T1
&nbsp; &nbsp; &nbsp; &nbsp; ON T0.owner = T1.owner
&nbsp; &nbsp; &nbsp; &nbsp;AND T0.table_name = T1.table_name
&nbsp; &nbsp; &nbsp; &nbsp;AND TRIM(T0.constraint_name) = TRIM(T1.constraint_name)
&nbsp; &nbsp; &nbsp;WHERE T0.owner = cv_owner
&nbsp; &nbsp; &nbsp; &nbsp;AND TRIM(T0.column_name) = TRIM(cv_indice)
&nbsp; &nbsp; &nbsp; &nbsp;AND T1.constraint_type = 'P';
&nbsp; ---------------------------------
&nbsp; CURSOR C_TRIGGER(cv_nome_tabela IN VARCHAR2) IS
&nbsp; &nbsp; SELECT unique T.trigger_name
&nbsp; &nbsp; &nbsp; FROM ALL_TRIGGERS T
&nbsp; &nbsp; &nbsp;WHERE T.owner = v_owner
&nbsp; &nbsp; &nbsp; &nbsp;AND T.table_name = cv_nome_tabela;
BEGIN
&nbsp; -----------------------
&nbsp; -- CARREGA AS SEQUENCES DO OWNER
&nbsp; -----------------------
&nbsp; FOR RC_SEQ IN C_SEQUENCE LOOP
&nbsp; &nbsp; -- LIMPA VARIAVEIS -- 
&nbsp; &nbsp; v_sql_seq_c := '';
&nbsp; &nbsp; v_sql_seq_d := '';
&nbsp; &nbsp; v_sql_seq_m := '';
&nbsp; &nbsp; v_sql_trg &nbsp; := '';
&nbsp; &nbsp; -- BUSCA O NOME DA TABELA
&nbsp; &nbsp; BEGIN
&nbsp; &nbsp; &nbsp; v_nome_tabela := '';
&nbsp; &nbsp; &nbsp; FOR rc_tabela IN C_TABELA(v_owner, RC_SEQ.nome_sequence) LOOP
&nbsp; &nbsp; &nbsp; &nbsp; -- PEGA O MAIOR ID
&nbsp; &nbsp; &nbsp; &nbsp; v_sql_seq_m &nbsp; := 'SELECT NVL(MAXID,0) FROM(SELECT MAX(' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRIM(RC_SEQ.nome_sequence) || ') MAXID FROM ' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rc_tabela.tabela || ')';
&nbsp; &nbsp; &nbsp; &nbsp; v_nome_tabela := upper(trim(rc_tabela.tabela));
&nbsp; &nbsp; &nbsp; END LOOP;
&nbsp; &nbsp; &nbsp; --------------------------------
&nbsp; &nbsp; &nbsp; IF v_sql_seq_m IS NOT NULL THEN
&nbsp; &nbsp; &nbsp; &nbsp; EXECUTE IMMEDIATE v_sql_seq_m
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INTO v_maxID;
&nbsp; &nbsp; &nbsp; END IF;
&nbsp; &nbsp; &nbsp; -- DROPA A SEQUENCE
&nbsp; &nbsp; &nbsp; v_sql_seq_d := 'DROP SEQUENCE ' || v_owner || '.' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RC_SEQ.nome_sequence;
&nbsp; &nbsp; &nbsp; IF v_sql_seq_d IS NOT NULL and v_sql_seq_m is not null and
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v_nome_tabela is not null THEN
&nbsp; &nbsp; &nbsp; &nbsp; EXECUTE IMMEDIATE v_sql_seq_d;
&nbsp; &nbsp; &nbsp; END IF;
&nbsp; &nbsp; &nbsp; -- RECRIA A SEQUENCE
&nbsp; &nbsp; &nbsp; v_maxID &nbsp; &nbsp; := (v_maxID + 1);
&nbsp; &nbsp; &nbsp; v_sql_max &nbsp; := TO_CHAR(v_maxID);
&nbsp; &nbsp; &nbsp; v_sql_seq_c := 'CREATE SEQUENCE ' || v_owner || '.' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RC_SEQ.nome_sequence || ' MINVALUE 1' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;' MAXVALUE 9999999999999999999999999999' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;' START WITH ' || v_sql_max || ' INCREMENT BY 1' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;' CACHE 20';
&nbsp; &nbsp; &nbsp; IF v_sql_seq_c IS NOT NULL and v_nome_tabela is not null THEN
&nbsp; &nbsp; &nbsp; &nbsp; EXECUTE IMMEDIATE v_sql_seq_c;
&nbsp; &nbsp; &nbsp; END IF;
&nbsp; &nbsp; &nbsp; -- RECOMPILA AS TRIGGERS VINCULADAS AS TABELAS
&nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; FOR RC_TRG IN C_TRIGGER(v_nome_tabela) LOOP
&nbsp; &nbsp; &nbsp; &nbsp; BEGIN
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v_sql_trg := 'ALTER TRIGGER "' || v_owner || '"."' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RC_TRG.trigger_name || '" COMPILE';
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EXECUTE IMMEDIATE v_sql_trg;
&nbsp; &nbsp; &nbsp; &nbsp; EXCEPTION
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN OTHERS THEN
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v_erro := 'TRIGGER - ERRO: ' || SQLCODE || ' - ' || SQLERRM;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DBMS_OUTPUT.put_line(v_erro);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DBMS_OUTPUT.put_line(v_sql_trg);
&nbsp; &nbsp; &nbsp; &nbsp; END;
&nbsp; &nbsp; &nbsp; END LOOP;
&nbsp; &nbsp; &nbsp; -------------------------
&nbsp; &nbsp; EXCEPTION
&nbsp; &nbsp; &nbsp; WHEN NO_DATA_FOUND THEN
&nbsp; &nbsp; &nbsp; &nbsp; v_erro := 'A SEQUENCE: ' || v_owner || '.' || RC_SEQ.nome_sequence ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' NÃO FOI CORRELACIONADA COM NENHUMA TABELA!';
&nbsp; &nbsp; &nbsp; &nbsp; DBMS_OUTPUT.put_line(v_erro);
&nbsp; &nbsp; &nbsp; WHEN OTHERS THEN
&nbsp; &nbsp; &nbsp; &nbsp; v_erro := 'SEQUENCE - ERRO: ' || SQLCODE || ' - ' || SQLERRM;
&nbsp; &nbsp; &nbsp; &nbsp; DBMS_OUTPUT.put_line(v_erro);
&nbsp; &nbsp; &nbsp; &nbsp; DBMS_OUTPUT.put_line('ERRO: CRIAÇÃO SEQ -&gt; ' || v_sql_seq_c ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;' TABELA -&gt; ' || v_nome_tabela);
&nbsp; &nbsp; END;
&nbsp; END LOOP;
&nbsp; COMMIT;
EXCEPTION
&nbsp; WHEN OTHERS THEN
&nbsp; &nbsp; v_erro := 'GENÉRICO - ERRO: ' || SQLCODE || ' - ' || SQLERRM;
&nbsp; &nbsp; DBMS_OUTPUT.put_line(v_erro);
END;



Espero que ajude, Fiquem na Paz.