Re: How to get the auto_increment generated ID of an INSERT operation

3,812 views
Skip to first unread message

Noel L

unread,
Aug 7, 2012, 1:00:51 AM8/7/12
to h2-da...@googlegroups.com
Hi Ugo,

In tables where I have used an auto generated ID, the latest ID is always the highest number.  So I use SELECT max(ID) FROM <filename>.  There may be a better way to do it, but this works fine for me.

Noel

On Tuesday, August 7, 2012 3:41:48 AM UTC+10, Ugo Matrangolo wrote:
Hi,

I need to know the auto generated ID of the last entity that was just persisted on the db using an INSERT operation.

On PostGRES I was used to the `... returning id` statement at the end of the INSERT operation. This seems to be missing from H2. 

How can I get the same behavior ???

Cheers,
Ugo.

Thomas Mueller

unread,
Aug 7, 2012, 4:29:38 PM8/7/12
to h2-da...@googlegroups.com
Hi,

The standardized way is using the JDBC API, Statement.getGeneratedKeys:


For MS SQL Server compatibility, H2 also supports IDENTITY and SCOPE_IDENTITY:


Regards,
Thomas


--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/6XUyWiID7rAJ.

To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Reply all
Reply to author
Forward
0 new messages