【问题标题】:Reset sequence to a specific value将序列重置为特定值
【发布时间】:2014-04-17 09:44:08
【问题描述】:

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

你能做相当于子选择和计算的方法来获取值还是需要将其设置为变量 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 的序列结束。

【问题讨论】:

标签: sql oracle oracle11g sequence


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多