Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last insert ID from Access

I am familiar with the MySQL function LAST_INSERT_ID; is there a similar function for performing the same query with a MS Access database via ODBC?

In my specific case, I am using PHP+PDO to insert rows into an Access database, and would like to know the last primary key value of each insert as they are performed.

If this functionality is not available, are there any alternatives? (without changing the database)

Thank you.

like image 885
Mikuso Avatar asked Jul 22 '26 19:07

Mikuso


1 Answers

It seems that Access 2000 or later supports the @@IDENTITY property. So, you would only need to select its value after an INSERT:

select @@IDENTITY from myTable

Please see the MSDN link: Retrieving Identity or Autonumber Values

In short:

[...] Microsoft Access 2000 or later does support the @@IDENTITY property to retrieve the value of an Autonumber field after an INSERT. Using the RowUpdated event, you can determine if an INSERT has occurred, retrieve the latest @@IDENTITY value, and place that in the identity column of the local table in the DataSet.

like image 118
Will Marcouiller Avatar answered Jul 24 '26 08:07

Will Marcouiller