如何在 Oracle 中重置序列?

2021-12-01 00:00:00 sql sequence database oracle

在 PostgreSQL 中,我可以这样做:

In PostgreSQL, I can do something like this:

ALTER SEQUENCE serial RESTART WITH 0;

是否有 Oracle 等价物?

Is there an Oracle equivalent?

推荐答案

这里有一个很好的过程,可以将 Oracle 大师的任何序列重置为 0 汤姆·凯特.在下面的链接中也对利弊进行了很好的讨论.

Here is a good procedure for resetting any sequence to 0 from Oracle guru Tom Kyte. Great discussion on the pros and cons in the links below too.

tkyte@TKYTE901.US.ORACLE.COM> 
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;
/

从此页面:动态用于重置序列值的 SQL
另一个很好的讨论也在这里:如何重置序列?

相关文章