When I bind an Oracle Sequence to an Item it "jumps" a lot of numbers.
When I do some inserts it does not "jump". It create the numbers on the sequence,
but when I insert in another day it "jumps". For example: yesterday I insert four rows, 1 to 4.
Today I insert other 3 rows. It begins with 21 and goes until 23.
Is there any way I can change this behavior?
Using a WHEN-CREATE-RECORD trigger performing a SELECT SEQUENCE.NEXTVAL from
dual to increment the sequence by 1 and assign a value.
You are getting "gaps" in the sequence numbers.
How can this be avoided?
You may have gaps in the sequence numbers if you have not saved the
dat after getting a new value from the sequence.
Some hints:
1.Move code in which you are accessing Sequence to a PRE-INSERT trigger.
You may still get a gap in the event of a system ROLLBACK,
but in general this will greatly minimize the gaps.
2. Alter the sequence so it does not cache values in memory with
the alter sequence <sequence_name> NOCACHE command.
3. Select the MAX value from the appropriate column instead of Sequence and
increment it in a PRE-INSERT trigger .
4. Move Forms code from WHEN-CREATE-RECORD trigger to a PRE-INSERT
trigger. You may still get a gap in the event of a system ROLLBACK, but in
general this will greatly minimize your gaps.
Using the sequence as the default value may waste sequence
numbers because another sequence number is used whenever the CREATE RECORD
event occurs, even if the data is never saved to the database. Thus, you
may have gaps between the sequence numbers of data actually saved to the
database
To use a sequence in a Forms application you must create the sequence database
object, then access the integer value the sequence generates within Forms.
1. Create a database sequence:
You can create a database sequence using SQL*Plus, and you do not have to
connect with DBA privileges to create a sequence in your own schema.
sqlplus scott/tiger
a. Create a database sequence using the CREATE SEQUENCE SQL command.
SQL> CREATE SEQUENCE myseq INCREMENT BY 1 START WITH 100 ORDER;
Sequence created.
b. Grant SELECT privileges on the sequence to other users. The SELECT
privilege allows other users to access and increment the values of
the sequence.
SQL> GRANT SELECT ON myseq TO PUBLIC;
Grant succeeded.
c. Create a SYNONYM for the sequence. A SYNONYM will allow you to hide any
schema and database link information related to the sequence.
SQL> CREATE PUBLIC SYNONYM myseq FOR scott.myseq;
Synonym created.
See the SQL Language Reference Manual for a complete description of the
CREATE SEQUENCE, GRANT and CREATE SYNONYM SQL commands.
2. Access the sequence value from Forms:
There are two ways to access a sequence within a Forms application: in the
default value property of a form item, and using a SELECT...INTO statement
in PL/SQL.
a. Default Value ( Item ):
-----------------------
Set the default value property for the form item to:
:SEQUENCE.myseq.NEXTVAL
Using the sequence as the default value may waste sequence numbers
because another sequence number is used whenever the CREATE RECORD event
occurs, even if the data is never saved to the database. Thus, you may
have gaps between the sequence numbers of data actually saved to the
database.
The On-Sequence-Number trigger fires when a sequence is specified as the default value for an item. Use this
trigger only if you want to execute additional code when the sequence
number is accessed. This trigger overrides the default processing, so
you must include a call to the GENERATE_SEQUENCE_NUMBER built-in to get
the next value from the sequence. If you do not want to execute
additional code when the next value of the sequence is retrieved, do not
create an 0n-Sequence-Number trigger.
b. SELECT...INTO statement in PL/SQL:
----------------------------------
DECLARE
variable_name INTEGER;
BEGIN
SELECT myseq.NEXTVAL INTO variable_name FROM DUAL;
END;
You can create a PRE-INSERT trigger at BLOCK level, and include:
SELECT myseq.nextval INTO :block.item from DUAL;
The Pre-Insert trigger will only retrieve the next sequence number when
the operator does a save/commit to the database. Thus, there will be
less gaps in the sequence numbers.