Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I get the unique identifier value of a record inserted into an Autonumber field?

0
Posted

How do I get the unique identifier value of a record inserted into an Autonumber field?

0

It depends what DBMS you are using. The simplest way is to write an ON INSERT trigger which returns the value of @@IDENTITY when you do the insert. This is only possible in SQL Server. You may want to use a stored procedure instead (see below). If you are using another DBMS which supports stored procedures, eg Oracle, you can wrap the insert into a stored procedure and return the ID from that. If the DBMS does not support stored procedures (eg Access), you have to make another query to the database immediately after the INSERT in order to retrieve the ID. One way is to SELECT MAX(ID). The problem here is that if another user has added a record between the two SQL operations, you will get the wrong ID back. This can be avoided by wrapping the two operations inside a database transaction. Alternatively, you can SELECT ID …WHERE (all the field values match). This is less efficient but could be necessary if the DBMS does not support transactions. An alternative to all these is to avoid u

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123