Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get an auto increment from a database

Status
Not open for further replies.

Wiszh

Programmer
Aug 3, 2000
134
US
Hi,
Does anyone know of a simple means to retrive a auto_increment primary key field after the insert takes place (so a subsequent update can be performed)?

Thanx
 
Well, most systems just do:

select max(auto_inc_field) as mac from db.table
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Hi Wishz,

You would want to find the specific function that the backend database offers to return that value.

For instance on MS SQL Server:
Code:
Insert into tablename ......
Select @@IDENTITY
will return to you the value that you're after. The value returned is guaranteed to be correct for your connection, even if other people are simultaneously inserting.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top