The autonumber is not assigned until the record is inserted. You can always query the most recent autonumber by using the following:
Select Top 1 RecordID from YourTable
Regards,
meapledoo
Main Topics
Browse All TopicsIs there a way to get the current record id of a data item that you are adding? My problem is that I am inserting a value into a database that has a autonumber as the key. Is there a way to get the value of that key easily? What I am having to do is go back after I add it and query for a value that has all the conditions of the data I just added.
It seems there should be some sort of reference to the data you just added without having to go back and query again.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
well the above single query does all the work instead of either running other query where either you reverse the table and get the top one record or get the max autonumber
you can use the query as
select top 1 recordid from yourtable order desc
though you have to do this in next query.
Regards,'
---Pinal
I am writing now the code of the alternate method which I discussed above.
<cfquery datasource="#DSN#" name="staff_detail">
insert into staff(name) values ("Pinal Dave");
</cfquery>
<cfquery datasource="#DSN#" name="staff">
SELECT TOP 1 * FROM Staff_contact
ORDER BY id DESC
</cfquery>
This is alternate method.
Regards,
---Pinal
to use that variable you can use the query like following
<cfquery datasource="#dsn#">
declare @incomingmailid int;
insert into incomingmail (contactid) values (#contactid#);
select @incomingmailid = @@identity;
insert into interactions (incomingmailid)
values (@incomingmailid);
</cfquery>
Regards,
---Pinal
PS. see the usage of the variable @@identity
Please note @@Identity only works with SQL server not all databases
In general a good method is to have a unique identity (like row number) an autonumber or identity field,etc
Then enclose two separate queries (a insert followed by a select) inside a set of cftransaction tags.
This is the best cross-platform method available
<cftransaction action="begin">
<cfquery datasource="mydsn">
INSERT INTO mytable (col1, col2, col3, etc) VALUES (#form.col1#, #form.col2# #form.col3#, #form.etc#)
</cfquery>
<cfquery datasource="mydsn" name="GetnewID">
SELECT MAX(ID) FROM mytable
</cfquery>
<cfset NewID = GetnewID.ID>
<cftransaction action="commit">
</cftransaction>
Business Accounts
Answer for Membership
by: pinaldavePosted on 2004-06-18 at 19:43:33ID: 11349577
yes sir,
wonderful Q. I just have done this today morning!!!
<cfquery datasource="#dsn#">
declare @incomingmailid int;
insert into incomingmail (contactid) values (#contactid#);
select @incomingmailid = @@identity;
insert into interactions (incomingmailid)
values (@incomingmailid);
</cfquery>
I have given you complite query which is working in my system.
I am sure this helps.
Regards,
---Pinal