4

我在开发环境中有一些插入要发布,开发人员在进行插入时没有使用序列来获取正确的标识符。他所做的是进入生产数据库,找出最大的标识符是什么,并在其中添加了几百个数字。为了启动他现在已经离开了公司。

所以 prod 的脚本充满了数百个:

INSERT INTO table (sequencecol, cola, colb) VALUES (90001,'test','test');

INSERT INTO childtableB (foreignkeyTableA, cola) VALUES (90001,'test')

应该是什么时候

INSERT INTO tableA (sequencecol, cola, colb) VALUES (tablesequence.NEXTVAL,'test','test');
INSERT INTO childtableB (foreignkeyTableA, cola) VALUES (tablesequence.CURRVAL,'test')

我正在处理的场景比这更复杂,我没有时间重写,相反,我只想将序列设置为作为该脚本的一部分插入的记录的最大值。

是否可以更改序列的值以将其设置为我要插入的记录的最大值 +1 的值?

我愿意接受更好的建议

4

1 回答 1

11

方法很好。

但是看看这个相关问题的答案,仅仅将一个序列重置为一个特定的值并不容易。

由于您不需要回到这里,也许只需编写一个小脚本,在循环中使用序列中的数字,直到它到达您需要的位置。

或者,如果它有很多数字:

-- First, alter the object so the next increment will jump 1000 instead of just 1.
alter sequence myschema.seq_mysequence increment by 1000;

-- Run a select to actually increment it by 1000
select myschema.seq_mysequence.nextval from dual;

-- Alter the object back to incrementing only 1 at a time
alter sequence myschema.seq_mysequence increment by 1;
于 2012-06-08T04:49:54.923 回答