In an Oracle PLSQL function I want to generate a special kind of sequence number. It starts with 00000001
, but when 00899999
is reached, I want to have 01000001
as its next number. This pattern repeats when 01899999
is reached: The next number then should be 02000001
.
To put it another way: My goal is to skip the numbers in the sequence in the range between ##900000
and ##999999
, where ##
denote two digits.
How can this be done on an Oracle database?
Best Answer
If you're looking for an algorithm:
So:
One implementation:
CREATE TABLE myNumSeq (lastNum int); INSERT INTO myNumSeq (0);
Create a function,
fn_GetNextNum()
, that works something like this:Add error handling as appropriate. I haven't used Oracle regularly in about 18 years, so please consider the above pseudocode. I leave the Oracle-specific details (can't recall if it has both procedures and functions, or the return mechanism used for something like this if it only has procedures) to someone familiar with that.
I'm sure this could be written more concisely, but for single-record additions it should work fine. If you're adding records in bulk, using a variation on a number table would probably be quicker than a function.