Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将序列重置为特定值_Sql_Oracle_Oracle11g_Sequence - Fatal编程技术网

Sql 将序列重置为特定值

Sql 将序列重置为特定值,sql,oracle,oracle11g,sequence,Sql,Oracle,Oracle11g,Sequence,我们正在创建现有数据库的“空白”或最小副本,并希望将其中一个序列重置为值。将数字放在下面是可行的,但我想在导出中的序列具有更高的数字时使其可重用,以避免删除和重新创建 您可以执行与子选择和计算等效的操作来获取值,还是需要将其设置为变量 alter sequence users.SQ_USER_ID INCREMENT BY (99999 - select users.SQ_USER_ID.nextval from dual) nocache; select users.SQ_USER_ID.n

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

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

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结束

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