Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the value of an Oracle sequence non sequentially?

Tags:

sql

oracle

plsql

I have some inserts in a development environment to release, the developer has not used sequences to obtain the correct identifiers when doing inserts. What he has done is gone into the production database and found out what the largest identifier is and added a few hundred numbers to it. To boot he has now left the company.

So the script to prod is filled with hundreds of these:

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

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

When it should have been

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

The scneario i am dealing with is more complicated than this, and i dont have the time to rewrite, instead i just want to set the sequence to the max value of the records inserted as part of this script.

Is it possible to alter the value of a sequence to set it to the value of the highest value +1 of the records i am to insert?

I'm open to better suggestions

like image 400
Neophyte.net Avatar asked Dec 20 '22 20:12

Neophyte.net


1 Answers

The approach is good.

But looking at the answers to this related questions, it is not easy to just reset a sequence to a particular value.

Since you don't need to go back here, maybe just write a small script that in a loop consumes numbers from the sequence until it's where you need it to be.

Or, if it is a lot of numbers this:

-- 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;
like image 103
Thilo Avatar answered May 13 '23 23:05

Thilo