小编典典

将序列重置为特定值

sql

我们正在创建现有数据库的“空白”
/最小副本,并希望将序列之一重置为值。将数字放在下面的作品中,但是当导出中的序列具有更高的数字时,我想使其可重用,从而尝试避免删除和重新创建。

您是否可以执行子选择和计算的等效操作来获取值,还是需要将其设置为变量1st?

alter sequence users.SQ_USER_ID INCREMENT BY  (99999 - select users.SQ_USER_ID.nextval from dual) nocache;
select users.SQ_USER_ID.nextval from dual;
alter sequence users.SQ_USER_ID INCREMENT BY 1  cache 20;

目的是以nextval的序列结尾为99999。


阅读 258

收藏
2021-04-14

共1个答案

小编典典

您可以使用负增量将序列重置为较低的值-此脚本(仅是您的PL / SQL块版本)可用于大于9999的序列值而不会出现问题):

declare
 currval pls_integer;
 diff pls_integer;
begin
  select SQ_USER_ID.nextval into currval from dual;
  dbms_output.put_line('value before alter: ' || currval);
  diff := 99999 - currval;
  dbms_output.put_line('diff: ' || diff);
  execute immediate ' alter sequence SQ_USER_ID INCREMENT BY ' ||  diff || 'nocache';
  select SQ_USER_ID.nextval into currval from dual;
  dbms_output.put_line('value after alter: ' || currval);
  execute immediate 'alter sequence SQ_USER_ID INCREMENT BY 1  cache 20';
end;
2021-04-14