Home >Database >Mysql Tutorial >How Can I Reset an Oracle Sequence to Zero?

How Can I Reset an Oracle Sequence to Zero?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-20 10:52:11186browse

How Can I Reset an Oracle Sequence to Zero?

Oracle Sequence Resetting Techniques

This article details how to reset an Oracle sequence to zero. A common method involves a custom procedure that first retrieves the sequence's current value, then decrements it to the minimum value (zero in this case), and finally resets the increment back to one. This ensures the sequence restarts from zero while maintaining its integrity.

Oracle expert Tom Kyte's efficient procedure for resetting a sequence to 0 is shown below:

<code class="language-sql">create or replace procedure reset_seq(p_seq_name in varchar2)
is
  l_val number;
begin
  execute immediate 'select ' || p_seq_name || '.nextval from dual' into l_val;
  execute immediate 'alter sequence ' || p_seq_name || ' increment by -' || l_val || ' minvalue 0';
  execute immediate 'select ' || p_seq_name || '.nextval from dual' into l_val;
  execute immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
end;
/</code>

To utilize this procedure, simply pass the sequence name as a parameter:

<code class="language-sql">reset_seq('my_sequence');</code>

Following execution, the 'my_sequence' sequence will be reset to 0, ready to generate unique values sequentially from this point.

The above is the detailed content of How Can I Reset an Oracle Sequence to Zero?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn