cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve row ID after insert?

Former Member
0 Kudos

Hi all!

  I have a table that has 3 columns: ID, Name, LastVisit, wich are int, string, timestamp.  I'm inserting data in this table and I need to retrieve the ID I just inserted. Problem is that the ID is a sequence. What I am trying to do is:

var insertCmd = connection.prepareStatement('insert into "Test"."TestTable"("ID", "Name", "LastVisit") values(seq.NEXTVAL,?,?)');

insertCmd.setString(1,"Rodrigo");

insertCmd.setString(2,"2015-06-22 00:00:00");

var test = insertCmd.execute();

var rowId = test.getString(1);

  But I cannot get the rowId like I need to. Is there a way I can retrieve it? Thanks in advance!!

Accepted Solutions (1)

Accepted Solutions (1)

pfefferf
Active Contributor
0 Kudos

Hello Rodrigo,

did you try to use CURRVAL of the sequence? Within the same session CURRVAL returns the number determined by the previous executed NEXTVAL. Because sequences are handled session dependent the value is also not influenced by another sessions (e.g. if your program is executed in another session and increases the sequence counter).

Execute statement "SELECT seq.CURRVAL FROM DUMMY" via the same connection after your insert statement.

Best Regards,

Florian

Former Member
0 Kudos

Thanks, that worked perfectly!!!

Answers (0)