ORACLE开发:oracle重置序列从0开始递增1

oracle序列创建以后,如果想重置序列从 0 开始,逐渐递增1,可以采用如下存储过程:

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;

存储过程创建以后,调用该存储过程,参数是要重置的序列名称:

call reset_seq(‘test_seq’);

使用下面的SQL语句查询需要重置的序列:

SELECT a.sequence_name 序列名称,
a.min_value 序列最小值,
to_char(to_number( a.max_value)) 序列最大值,
a.last_number 序列当前值,
CASE a.last_number WHEN 1 THEN ‘–不需要重置;’ ELSE
‘CALL seq_reset(”’ || a.sequence_name || ”’);’ END 重置序列脚本,
‘DROP SEQUENCE ‘ || a.sequence_name 删除序列脚本,
‘RENAME ‘ || a.sequence_name || ‘ TO {newname}’ 重命名脚本
FROM USER_SEQUENCES a WHERE a.last_number<>1
ORDER BY a.sequence_name ASC;

到此这篇关于oracle重置序列的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。


数据运维技术 » ORACLE开发:oracle重置序列从0开始递增1