Oracle Sequence

I created a sequence and i am using a trigger on a field to generate the number, however there are existing number in the table that are within the sequence range
so i created to following code to look for existing numbers in the database then give a new sequence number. The sequence works fine but does not read the existing number in the database to give a new sequence number.
declare
v_pin_number_seq   varchar2(6);
CURSOR pin_no
   IS
   select pin_number from pin_request;
   pin_rec  pin_request.pin_number%type;
Begin
open pin_no;
fetch pin_no into pin_rec;
    SELECT PIN_NUMBER_SEQ.NEXTVAL
    INTO v_pin_number_seq
    FROM sys.dual;
     If v_pin_number_seq != pin_rec then
    :pin_number := v_pin_number_seq;
else
    SELECT PIN_NUMBER_SEQ.NEXTVAL
    INTO v_pin_number_seq
    FROM sys.dual;
End if;
   close pin_no;    
END;

Wrong forum, this is the Oracle Designer forum. You could ask this question inside the Oracle Database forum.
Not sure what you are trying to do? 
Do you have to re-sequence the table in the database? Or do you not want to prevent errors when the value of sequence exists inside the database,. You could also reset your sequence to the max value of the pin_number from the table.
The code above also does not loop over the table. It only checks the current value of the sequence against the current pin_number and it only increments the sequence once.

Similar Messages

  • Mixing sun-database-binding with Oracle sequences

    Hello,
    I wish to insert rows inside the database, for instance rows representing persons, and rows representing their addresses. Primary keys for both person and address are Oracle sequence based.
    My question is, if I insert a person, then one of her address, how can I retrieve the person ID for the address row to reference it.
    The insert statement for the person is (for the moment) as follow
    insert into Person (PERSON_ID, FIRST_NAME, LAST_NAME) values (PERSON_SEQ.nextval, ?, ?)The problem with this approach is that I never know the person ID, and am unable to make any references to it in the address row.
    I tried to add an operation in the NetBeans generated person table WSDL. This operation would execute this statement:
    select PERSON_SEQ.nextval from dualBut, for the moment, it's a failure.
    Could you provide me with some hints?

    Hi,
    First I would advise you to register and post on the [email protected] alias - you can find the details of how to do this on the OpenESB site..... you'll reach a larger audience this way.
    Second, you need a Stored Procedure in Oracle to do this, then use this from the DB BC, here's one I created which does something similar, i.e. returns a value I'm interested in after an "Update" statement....
    CREATE OR REPLACE PROCEDURE "NEXTAPPNUMFINDER" (nextAppNum OUT NUMBER)
    IS
    BEGIN
    UPDATE ACTIVE_APPLICATION_NUMBER
    SET APPLICATION_NUMBER_NEXT = APPLICATION_NUMBER_NEXT + 1
    RETURNING APPLICATION_NUMBER_NEXT
    INTO nextAppNum;
    END;
    Hope this helps
    Mark

  • Using oracle sequence in SQL Loader

    I'm using oracle sequence in control file of sql loader to load data from .csv file.
    Controlfile:
    LOAD DATA APPEND
    INTO TABLE PHONE_LIST
    FIELDS TERMINATED BY "," TRAILING NULLCOLS
    PHONE_LIST_ID "seqId.NEXTVAL",
    COUNTRY_CODE CHAR,
    CITY_CODE CHAR,
    BEGIN_RANGE CHAR,
    END_RANGE CHAR ,
    BLOCKED_FREE_FLAG CHAR
    Datafile:
    1516,8,9,9,B
    1517,1,1,2,B
    1518,8,9,9,B
    1519,8,9,9,B
    1520,8,9,9,B
    1521,8,9,9,B
    1) As first column uses oracle sequence, we have not defined that in datafile.
    This gives me error "Can not insert NULL value for last column"
    Is it mandatory to specify first column in datafile, even though we are using sequence?
    2) Another table is referencing PHONE_LIST_ID column (the one for which we using sequence) of this table as a foreign key.
    So is it possible to insert this column values in other table simultaneously? Sequence no. should be same as it is in first table...
    Kindly reply this on urgent basis....

    use BEFORE INSERT trigger
    with
    select your_seq.nextval into :new.id from dual;

  • JPA: Oracle Sequence Generator not up to date

    Hi,
    I'm using the JPA Oracle Sequence Generator in one of my JPA classes:
    @Entity
    @Table(name = "DACC_COST_TYPE")
    public class JPACostType implements Serializable {
    @SequenceGenerator(name = "CostTypeGenerator", sequenceName = "DACC_COST_TYPE_SEQ")
        @Column(name = "ID_COST_TYPE")
        @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CostTypeGenerator")
        private Integer idCostType;
    In order to persist a new object I perform the following code:
    @PersistenceContext
    private EntityManager em;
    JPACostType myJPA = new JPACostType();
    myJPA.setIdCostType = null;
    em.merge(myJPA);
    em.flush();
    Normally this works fine. But after deploying the app there sometimes happens an error:
    Caused by: javax.persistence.PersistenceException: SQLException while inserting entity {com.karmann.dacc.ejb.busilog.jpa.JPACostType(idCostType=4)}.
    at com.sap.engine.services.orpersistence.core.PersistenceContextImpl.flush(PersistenceContextImpl.java:278)
    at com.sap.engine.services.orpersistence.core.PersistenceContextImpl.beforeCompletion(PersistenceContextImpl.java:565)
    at com.sap.engine.services.orpersistence.entitymanager.EntityManagerImpl.beforeCompletion(EntityManagerImpl.java:410)
    at com.sap.engine.services.orpersistence.environment.AppJTAEnvironmentManager.beforeCompletion(AppJTAEnvironmentManager.java:197)
    at com.sap.engine.services.ts.jta.impl.TransactionImpl.commit(TransactionImpl.java:232)
    ... 52 more
    Caused by: java.sql.SQLException: ORA-00001: unique constraint (AEMA.DACC_COST_TYPE_PK) violated
    Obviously JPA does not fetch the new key by accessing the Oracle sequence. This documents "next value = 5". Does JPA fetch the new key from its cache? Is there any possibility to avoid this?
    Thanks for each hint,
    Christoph

    Hello Christoph Schäfer  ,
    I am stuck with a similar issue. I was able to save mutiple entries and there has not been much change to my JPA. I added new entities and new sequences.
    Now, I get the error Caused by: javax.persistence.PersistenceException: java.sql.SQLException: ORA-02289: Sequence ist nicht vorhanden.
    I have checked the name of sequence and sequence next val on the DB. It works on DB but when i execute it from ejb, it gives me thsi error. Now, it gives the error for all previously working JPA entities.
    I have also provided allocationSize = 1 for all entities.
    Please let me know, possible cause/solution to this issue.
    thank you.
    Regards,
    Sharath

  • Bind variable with Oracle sequence

    can we insert oracle sequence with the bind varaible
    some thing like
    seq       number(5);
    begin
    select myseq.nextval
            into seq        from dual;
    dbms_sql.parse( l_cursor,insert into mytable (c1,c2,c3) values (seq,:1,:2),dbms_sql.native );
    dbms_sql.bind_variable( l_cursor, :1,58);
    dbms_sql.bind_variable( l_cursor, :2,9);
    l_status := dbms_sql.execute( l_cursor );
    end;
    Thanks!

    What are you trying there..
    Why dynamic SQL?
    Where is the exception block?
    you have to use 'using ' to work with these kind of dynamic variables. Something like this (ugly code below)
    CREATE SEQUENCE myseq;
    create table mytable (c1 number,c2 number, c3 number;
    DECLARE
       seq   NUMBER (5);
       A number;
       b NUMBER;
    BEGIN
       SELECT myseq.NEXTVAL INTO seq FROM DUAL;
       a := 1;
       b := 58;
       EXECUTE IMMEDIATE 'insert into mytable (c1,c2,c3) values (:seq,:1,:2)'
          USING seq, a, b;
       a := 1;
       b := 100;
       EXECUTE IMMEDIATE 'insert into mytable (c1,c2,c3) values (:seq,:1,:2)'
          USING seq, a, b;
       COMMIT;
    END;  --- Check you need to have proper exception block.
    Cheers,
    Manik.

  • Oracle sequence problem in CMP Entity EJB

    I have a problem with Oracle sequence when I am using from the CMP entity EJB in WebLogic
    6.1:
    The problem is when the WebLogic server starts it acquires the correct sequence number
    from Oracle, but during the runtime, when I open a new sqlplus window and increment
    the sequence number, the container is not picking up from the new incremented value
    instead it is still continuining by incrementing the old sequence number it has acquired
    when it fetched from Oracle last time.
    The sequence increment and key-cache-size in weblogic-cmp-rdbms-jar.xml are exactly
    same
    Any help would be greatly appreciated.

    Change the key-cache-size to 1 in weblogic-cmp-rdbms-jar.xml and then try
    your experiment again.
    -- Anand
    "Satya" <[email protected]> wrote in message
    news:3d06274c$[email protected]..
    >
    I have a problem with Oracle sequence when I am using from the CMP entityEJB in WebLogic
    6.1:
    The problem is when the WebLogic server starts it acquires the correctsequence number
    from Oracle, but during the runtime, when I open a new sqlplus window andincrement
    the sequence number, the container is not picking up from the newincremented value
    instead it is still continuining by incrementing the old sequence numberit has acquired
    when it fetched from Oracle last time.
    The sequence increment and key-cache-size in weblogic-cmp-rdbms-jar.xmlare exactly
    same
    Any help would be greatly appreciated.

  • Cannot get Oracle sequence to be used with SQL Loader direct path load.

    I attempted to load approximately 3 million rows from a flat file into a 10g database using SQL Loader direct path load. In order to generate a primary key for each of these rows I used a simple SQL expression which gets the next number of an Oracle sequence and assigned that to my primary key column inside the control file. The strange thing is no primary key was generated for each of these columns as I see the sequence was not advanced. Furthermore when I try to run a query to find the count on those rows which have this column as NOT NULL ,I get the total of all the rows even though on inspection this column is empty in each of the rows.
    As far as I know, in the control file for a direct path load I am able to use a SQL Expression that returns simple scalar data which a NEXTVAL on a sequence does. (Oreilly Oracle SQL Loader The Definitive Guide p.184)
    Does anybody why my primary key was not generated and furthermore why is this column in a query appear to be NOT NULL when in fact it has nothing in it?

    Daniel,
    I appreciate your response alot.
    Now a follow on. You say that SQL*Loader is the fastest way to get data into Oracle Spatial even though the conventional path. How does SQL*Loader do this? Doesn't it use OCI?
    I just can't help but think that coverting my data to SQL*Loader format, and then having it parse it and send it to the database in some form must be slower than me just sending whatever SQL Loader sends to the DB myself using OCI. Am I missing something?
    The SQL Loader documentation seems to suggest that SQL*Loader just turns the input into alot of INSERT stateemnts. If so, can't I just do this?
    My performance with OCI and INSERT statements isn't very good, but I believe I am doing one transaction per insert. Might I be as well off to concentrate on fine tuning my OCI based code using INSERTs?
    I will actually do some time tests myself, but I would appreciate your opinion.
    Once again thanks for the great info you have provided.

  • Bulk Fetch From an Oracle Sequence

    I am trying to get a range of sequence values from an Oracle sequence.
    I am using the option as show below using query
    SELECT SEQUENCE_NAME.NEXTVAL FROM SYS.DUAL CONNECT BY LEVEL <= 10.
    The above SQL gets 10 sequence value.
    I just wanted to to check, if the implementation below is safe in a Multi User Environment?
    Is the statement show below atomic. i.e. Multi parallel execution of the same function; Would it cause any inconsistencies?
    EXECUTE IMMEDIATE 'SELECT SEQUENCE_NAME.NEXTVAL ' ||
      'FROM SYS.DUAL CONNECT BY LEVEL <= ' || TO_CHAR(i_quantity)
      BULK COLLECT INTO v_seq_list;
    FUNCTION select_sequence_nextval_range(
       i_quantity      IN  INTEGER)
    RETURN INTEGER IS
      o_nextval INTEGER;
      v_seq_list sequence_list;
    BEGIN
      EXECUTE IMMEDIATE 'SELECT SEQUENCE_NAME.NEXTVAL ' ||
      'FROM SYS.DUAL CONNECT BY LEVEL <= ' || TO_CHAR(i_quantity)
      BULK COLLECT INTO v_seq_list;
      -- Get the first poid value.
      o_nextval := v_seq_list(1);
      RETURN o_nextval;
    END select_sequence_nextval_range

    Acquire Lock
    You acquire a lock on a sequence? That's news to me - please post the code that does that. I certainly hope you don' t mean you are directly accessing the SYS.SEQ$ table to lock the row for that sequence - it isn't nice to mess with Oracle's tables!
    For couple of JAVA/C applications the usage of sequence number is pretty big. Could be 100,000 for one single application processing.
    How does that correlate with your previous statement that you get 10 at a time?
    Sequences aren't designed for use cases that require gap-free sets of numbers or for use cases that require consecutive sets of numbers.
    We wanted to implement the range get of sequence using a different mechanism.
    For few other applications; we just need one sequence number for the application processing. So we use the select seq.nextval to get the value. So the same sequence number needs to serve the role of giving a single value as well as a consecutive range of values.
    Then you may need to consider using your own table to track the chunks that need to be allocated. You would use a scheme similar to what Greg.Spall discussed except you would keep the 'chunk' data in your own table.
    I'm not talking about using your own table to control actual one-by-one sequence number generation - that is a very bad idea. But if you need to work with large ranges that are allocated infrequently there is nothing wrong with using your own function and your own table to keep track of those allocations.
    The 'one by one' number generation would be handled by an actual sequence. The generation of a 'start value' and an 'end value' would be handled by accessing your custom table. Each row in that table would have 'start_value' and 'available_numbers' colulmns.
    Your function would take a parameter for how many numbers you need. For just one number the function would call the sequence.nextval and return that along with a count of '1'.
    For a range the function would:
    1. find a row in the table with an 'available_numbers' value large enough to satisfy the request,
    2. lock the row for update
    3. capture the 'start_value' for return to the user
    4. adjust both the 'start_value' and 'available_numbers' values to account for the range being allocated
    5. update the table and commit
    6. return the 'start_value' and 'number_allocated' to the user (number_allocated might be LESS than requested perhaps)
    The above is a viable solution ONLY if the frequency of allocation and the size of allocation avoids the serialization issues associated with trying to allocate your own sequence numbers.
    Those issues can be somewhat mitigated by having the table store multiple rows with each row having a large chunk of values that can be allocated. Then your function query can get the first 'unlocked' row and avoid serializing just because one row is currently locked.

  • How to use A RowSet with an Oracle sequence

    Hi,
    I created a RowSet with the RowSet wizard and it asked me if I wanted to use database created primary keys. I'm using Oracle so I selected this option and want to use Oracle sequences. But I don't find where should I configure the name of the Oracle sequence. Accordingly I cannot insert through the RowSet.
    Many thanks in advance.
    Nick

    b j t wrote:
    Pancenter wrote:
    That seems kind of strange...
    I have a 10.1" Acer Android tablet that can use a bluetooth and/or USB mouse and keyboard.
    I do realize that Android has "input" ports, the point I am making is that the iPad does not.
    The question the OP asked was:
    Why is there no application to run a mouse on an iPad? I would like to be able to work with Numbers and Pages withouit having to keep touching the screen.
    and I stand by the answer I gave which was:
    There is NO way to use a mouse with the iPad, you will have to either use your finger or a stylus to navigate around the screen.
    You may have to stand down. Youtube is swarmed with instructional video's about how to use Mouse (and keyboard) with an iPad. So there is a way. I don't understand why you insist on something when it clearly and easy Googly discoverably is not true.

  • Oracle Sequence Vs autoincrement feature in cold fusion-Weird error!!!

    When a oracle sequence is used with cold fusion there is a problem with sequence generation. It generates sequence
    like "5,4,6,5...."etc. It works fine when using the autoincrement feature. Our application demands using the sequence from oracle.
    Have you come across any problem as this? Can anyone tell me if there is any disadvantage using oracle sequence with cold fusion.
    Please let me know the disadvantages of the same if any...
    Thanks!
    Janani.

    This was definitely the right answer, I thought ODAC was already installed on the server, but apparently not. I installed the 64-bit OCAC XCopy version on the server and now it works.

  • OC4J -Time out and Oracle sequence problem

    hi
    i m using OC4J to access application which is using java as front and
    and we using oracle sequence with cache as 20.
    so when i accessing application thro' network my sequence skip from 20
    mean 1-21-41-61 is it possible due to if lost connection opened from java for oracle thro' OC4J ? will it keep cache ?or lost ? is it really time out error?
    if then hw to set time out in oracle?
    u can refer below link
    Re: ORABPEL-05002

    i think i may be having the same problem. My table is savings entries out of order. I would assume that each row saved would be in sequence number order...instead it goes from like 963, 964, 678, 679, ....??? wonder why it would place new entries in front of older entries in the table???

  • Oracle Sequence migration during an upgrade

    Hi,
    Firt of all Iam not a DBA, But we have a problem which I would like to know more about , we are planning on migrating to Oracle 8.1.7 from an older version and new Oracle Databases were created from the DUMP taken from old database..and when I was testing some functionality I found out that the SEQUENCES that were existing in the old database didn't get migrated to the new database...I had to create them inorder to test my application.
    My question is does oracle sequences get created automatically in this situation or we need to explicitly create them, because mu understanding was that all the objects get created automatically when you create it from DUMP.
    I would appreciate your help in this matter..
    Thanks,
    Ramki

    The project module interfaces with a lot of other modules in Oracle (Purchasing, GL, costing, and possibly OM and Manufacturing).
    For Projects implementation, you will have to test those modules.
    If you don't implement projects now, you will have to do a full blown regression testing for the upgrade and then again for the Projects implementation.
    The one disadvantage I see is that you will require more resources because you are doing 2 activities in parallel.
    Sandeep Gandhi

  • Back numbers in Oracle sequences

    We are using a J2EE based tool that uses Oracle as backend. For several purposes the tool use the oracle sequences. As per general rule the every time the sequence is referred, it generates a next higher number and returns the same for any kind of storage purposes like primary key, etc. But in rare cases we have got a previous number of the sequence instead of a next higher value. We aren't sure if that previous number was ever used before or not. But the problem is how does the oracle sequence return a previous number? e.g. we have a sequence running as 220, 221, 222, etc. then suddenly instead of returning 223 the next time, it may give the number 214 (or basically any previous number).
    Does anybody know why does that happen? What could be the possible reasons and how it can be avoided? It creates confusion amongst users and does not seem just right to get a previous number. Any help is appreciated. Thanks.

    Connection pooling should have no effect on this issue since the Oracle sequences should be cached in Oracle and not in the pooled session.
    RAC as mentioned would since each instance would have its own sequence cache: instance A values 1 - 5, instance B values 6 - 10, instance C 11 - 15.
    Then depending on which instance the user session was connected to the ticket numbers would appear to be selected from an earlier point in the sequence.
    On a non-RAC instance you can see this apparent behavior where the application gets the sequence number on the first screen and passes the value to later screens but the number is not inserted into a database table until the final screen that creates the ticket. If it takes the user longer to create the ticket than other users who started later then the ticket number would appear earlier than the earlier created tickets.
    The solution to this "problem" for non-RAC or RAC is to insert a timestamp at the time of ticket creation (or start) and to process the tickets in timestamp rather than ticket number order. After all the ticket number is just an identifier and there is really no logical reason that tickets have to be processed in ticket number order.
    If it is a political issue then you could use a dual key system. Assign one key at creation time, transaction key, and then assign the ticket number via batch based on either the timestamp of when ticket entry started or was submitted. Now the ticket numbers represent whatever order you need. You can notify the users of the ticket number via email.
    HTH -- Mark D Powell --

  • JDeveloper 10g and Oracle Sequences

    Hello,
    i am new to Java/JDeveloper and I would like to know,
    how I can use an existing oracle sequence in JDeveloper to
    create a value for a field in my table.
    (using JDBC, no "Toplink")
    Greetings.

    Hi,
    you could issue a select <sequence>.nextval from dual query statement and obtain the result from the return set.
    Frank

  • Oracle Sequence Numbers

    I have a unit of work where I'm adding a record to 2 different tables. The first table uses a oracle sequence number for the primary key. The second table needs the number as well. I use the assignSequenceNumber method and I get the number before the commit. The second table has the correct number. The first table has the next sequence number available. (+1) I look at the messages in the weblogic log and toplink does a nextval from dual in the uow before the commit. (number used by second table) It does another nextval from dual during the commit. (second table has number + 1) Does anyone know why this is happening?
    Thanks in advance.

    By deafault, TopLink assigns sequence number during the commit. However, if you assign them yourself first, TopLink will not assign the numbers during commit. Check the objects before you call commit and make sure that neither of them have a null (or 0 in the case of a primitive) primary key.
    Peter Krogh

  • Creating ORACLE sequence

    Hi,
    I have a variable called ID. Now I want to insert values into a talble which has this ID field. I have intialised this variable to 1. I want to create an oracle sequence for this field(ID) so that I can use ID.nextval in my insert statement in order to populate this field.
    How can I do it.
    Thanks in advance

    Did you search before you post this? An example for how to create and use sequence is given below.
    SQL> CREATE TABLE T(ID NUMBER(10));
    Table created.
    SQL> CREATE SEQUENCE SEQ_ID
      2      START WITH 1
      3      INCREMENT BY 1
      4      NOMINVALUE
      5      NOMAXVALUE
      6      NOCYCLE
      7      CACHE 20
      8      NOORDER
      9  /
    Sequence created.
    SQL> INSERT INTO T VALUES(SEQ_ID.NEXTVAL);
    1 row created.
    SQL> INSERT INTO T VALUES(SEQ_ID.NEXTVAL);
    1 row created.
    SQL> INSERT INTO T VALUES(SEQ_ID.NEXTVAL);
    1 row created.
    SQL> INSERT INTO T VALUES(SEQ_ID.NEXTVAL);
    1 row created.
    SQL> INSERT INTO T VALUES(SEQ_ID.NEXTVAL);
    1 row created.
    SQL> select * from t;
            ID
             1
             2
             3
             4
             5
    SQL>

Maybe you are looking for