Effective way to reset Oracle sequence to zero
PostgreSQL uses the ALTER SEQUENCE
command in conjunction with the RESTART
option to efficiently reset the sequence. However, there is no direct corresponding command in Oracle.
Oracle solution: Custom process
Oracle expert Tom Kyte provides a powerful procedure for resetting a sequence to 0. This process consists of the following steps:
Determine the current sequence value:
l_val
). Adjust sequence increment:
Revert changes:
Example process:
The following customization process reset_seq
summarizes the above steps:
<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>
More resources:
The above is the detailed content of How to Efficiently Reset an Oracle Sequence to Zero?. For more information, please follow other related articles on the PHP Chinese website!