Results 1 to 7 of 7

Thread: How to get sequence number of last data entry

  1. #1
    Join Date
    Sep 2009
    Posts
    16

    Default How to get sequence number of last data entry

    Hi all

    I am writing to an Oracle database and as potentially my insert statement contains e.g. 100 rows to insert. I need to set the sequence number of the input. Is there a way of getting the last entry written to the database and adding my new row to that ID + 1 in Spring?

    Thanks for any help

  2. #2
    Join Date
    May 2008
    Posts
    15

    Default

    hi,

    I guess you are using a sequence (for generating your ids)? If so assuming that

    last-sequence-value + 1 == next-sequence-value

    is generally wrong (e.g. if you are using RAC).

    If you have the options to change/modify the database you could just use a trigger which sets the id accordingly - something like this:

    Code:
    CREATE OR REPLACE TRIGGER "MYSCHEMA"."MYTRIGGER" 
    BEFORE INSERT ON MYSCHEMA.MYTABLE    
    FOR EACH ROW
    BEGIN
          IF :new.id IS NULL THEN
             SELECT MYSEQUENCE.nextval INTO :new.id FROM DUAL;
          END IF;
    END;
    ALTER TRIGGER "MYSCHEMA"."MYTRIGGER" ENABLE
    Alternatively, you could also retrieve the nextval of your sequence within your select statement like this:

    Code:
    insert into myschema.mytable (id) values (myschema.mysequence.nextval);
    daniel

  3. #3
    Join Date
    Sep 2009
    Posts
    16

    Default

    Thanks for your suggestion...I have checked the database in Oracle SQL Developer and under the Sequences section there definitely is a sequence which exists for my table - the definition of this sequence is as follows:

    Code:
    CREATE SEQUENCE  "Test_UserName"."MY_TABLE_SEQ"  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER  NOCYCLE ;
    Now in my Java database class I am doing the following:

    Code:
    String seq = Test_UserName.MY_TABLE_SEQ";
     jdbcTemplate
                    .update(
                            "insert into TEST_TABLE (Id, Year_Month, Year_Day) values (?, ?, ?)",
    new Object[] { sequence, "31-Mar-2009", 12});
    This is throwing an exception as the first value passed in the following:

    Code:
    new Object[] { sequence, "31-Mar-2009", 12});
    Is a String and therfore the call will fail. Whats the best way of getting the next sequence and passing this in as the first parameter by using:

    "myschema.mysequence.nextval"?

    Thanks again.

  4. #4

    Default How about....

    ...this:

    j
    Code:
    dbcTemplate.update(
                            "insert into TEST_TABLE (Id, Year_Month, Year_Day) values (sequence.nextval, ?, ?)",
    new Object[] { "31-Mar-2009", 12});

  5. #5
    Join Date
    Sep 2009
    Posts
    16

    Default

    Thats what I was doing in my last post - this is not allowed as the following error is thrown:

    ORA-01722: invalid number

    I think its expecting a number but passing in "sequence.nextval" is a String and not a number.

  6. #6

    Default Thats what I was doing in my last post

    No it isn't - one less ? so sequence.nextval is not passed as a parameter.

  7. #7
    Join Date
    Sep 2009
    Posts
    16

    Default

    Ahh perfect thankyou that works fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •