将序列重置为特定值

时间:2014-04-17 09:44:09

标签: sql oracle oracle11g sequence

我们正在创建现有数据库的“空白”/最小副本,并希望将其中一个序列重置为某个值。将数字放在下面的工作,但我想让它在导出中的序列具有更高的数字时可重复使用,试图避免丢弃&再现。

您是否可以使用相应的子选择和计算来获取值,还是需要将其设置为变量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.

1 个答案:

答案 0 :(得分:2)

您可以使用负增量将序列重置为较低的值 - 此脚本(它只是您的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;