我在开发环境中有一些插件要发布,开发人员在插入时没有使用序列来获取正确的标识符.他所做的就是进入生产数据库,找出最大的标识符是什么,并为其添加了几百个数字.为了引导他现在离开了公司.
因此,prod的脚本充满了数百个:
INSERT INTO table (sequencecol, cola, colb) VALUES (90001,'test','test');
INSERT INTO childtableB (foreignkeyTableA, cola) VALUES (90001,'test')
Run Code Online (Sandbox Code Playgroud)
什么时候应该
INSERT INTO tableA (sequencecol, cola, colb) VALUES (tablesequence.NEXTVAL,'test','test');
INSERT INTO childtableB (foreignkeyTableA, cola) VALUES (tablesequence.CURRVAL,'test')
Run Code Online (Sandbox Code Playgroud)
我正在处理的scneario比这更复杂,我没有时间重写,而只是想将序列设置为作为此脚本的一部分插入的记录的最大值.
是否可以更改序列的值以将其设置为我要插入的记录的最高值+1的值?
我愿意接受更好的建议
Thi*_*ilo 10
方法很好.
既然你不需要回到这里,也许只需编写一个小循环,在循环中消耗序列中的数字,直到你需要它为止.
或者,如果它是一个很大的数字这个:
-- 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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5926 次 |
| 最近记录: |