Just a shot in the dark, but you could use cftransaction, wrap all your cfqueries in a cftransaction and it should on treat all the querires as a single transaction.
Main Topics
Browse All TopicsHowdy,
Right now, the table in question has a primary key called `id`. We use MySQL and coldfusion. What i would like to know is how i can find the exact new record i have just inserted in a SQL query. For instance, this is how i do things now:
[CODE]
<cfquery datasource="myDSN">INSERT INTO posts SET name="#name#", body="#body#", date="#date", forumID="#forumID#"</cfque
<cfquery datasoure="mfDSN" name="current">SELECT id FROM posts WHERE name="#name#" AND date="#date#" AND forumID="#forumID#" LIMIT 1</cfquery>
[/CODE]
Now, there are obvious flaws with this system. If two users are creating exact post names at the exact same second (not unheard of), then its possible that my 'current' query may not actually be the one that was just inserted. Is there any good way to ensure i'm getting the correct record.
Thanks,
Travis
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.
Working off of Meps answer, yes you should put it into a <cftransaction> tag as this will kep the queries together. Within this though you should also call a SELECT statement selecting the largest ID. For instance:
<cftransaction>
<cfquery datasource="#dbsrc#" name="insert_record">
INSERT INTO tbl_users(username, fullName)
values('testuser', 'Test User')
</cfquery>
<cfquery datasource = "#dbsrc#" name="getNewId">
SELECT MAX(userId) as newUserID
FROM tbl_users
</cfquery>
</cftransaction>
<cfoutput>
#getNewId.newUserId#
</cfoutput>
I use bean and Transfer Objects for most of my data manip... but here is an example of what I do:
==========================
<cffunction name="create" access="public" returntype="void" output="false" hint="CRUD Method.">
<cfargument name="usersBean" type="users" required="true" hint="I am the users object from which to create a record." />/>
<cfset var usersInsert = 0 />
<cfset var usersmaxcols = 0 />
<cftransaction>
<cfquery name="usersInsert" datasource="#variables.dsn
INSERT INTO users
(
username, password, firstlogin, lastlogin, logins, alias, userTypeID, personnelID, active
)
VALUES (
<cfqueryparam cfsqltype="cf_sql_VARCHAR"
, <cfqueryparam cfsqltype="cf_sql_VARCHAR"
, <cfqueryparam cfsqltype="cf_sql_TIMESTAM
, <cfqueryparam cfsqltype="cf_sql_TIMESTAM
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_VARCHAR"
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_CHAR" value="#arguments.usersBea
)
</cfquery>
<cfquery name="usersmaxcols" datasource="#variables.dsn
select
max(userID) as maxuserID
from
users
</cfquery>
<cfset arguments.usersBean.setuse
</cftransaction>
</cffunction>
==========================
I use a cftransaction to wrap the insert and fetch and then set the userID of the bean with the results of the fetch. You could just as easily have a return and return the ID.
Couple of things, First off cftransaction is great but your DB has to support transactions and if you are using the free version of MySQL I don't believe it supports transactions so cftransaction doesn't do much for you. Second @@identtity works but it returns the last insert identity. Even inside of cftransaction its much safer to use SCOPE_IDENTITY() there is even another one where you specifify the entity name but I can't recall it at this time.
The best Way to know your ID is to make it yourself. Use createUUID() sure its 35 characters but in the end if you want a sure fire way that will work accross as many different DBs as you can think of then createUUID() is the best solution.
-cfMav
So,
is there anyway to know if my database supports transactions? Like, if i try <cftransaction>, how will i know that it was able to lock the database? will it throw and error or just run them as two consecutive queries?
- travis
http://www.iqdevs.net/
Take a look at this article while I investigate this a little more. Also what version of MySQL do you have?
http://www.samspublishing.
Okay, I am using version 4.1.11 of MySQL and this is how I perform a transactional write and retrieval of the last record. Again, I set to a bean object, so the idea for you would be the same, however, you would most likely just return a variable (string, integer, whatever the ID is).
==========================
<cffunction name="create" access="public" returntype="void" output="false" hint="CRUD Method.">
<cfargument name="articleBean" type="article" required="true" hint="I am the article object from which to create a record." />/>
<cfset var articleInsert = 0 />
<cfset var articlemaxcols = 0 />
<cftransaction>
<cfquery name="articleInsert" datasource="#variables.dsn
INSERT INTO article
(
art_Title, art_Contents, art_Author, art_Creation_Date, art_Published, art_MenuID, art_Order, art_Locked, art_Default
)
VALUES (
<cfqueryparam cfsqltype="cf_sql_VARCHAR"
, <cfqueryparam cfsqltype="cf_sql_LONGVARC
, <cfqueryparam cfsqltype="cf_sql_VARCHAR"
, <cfqueryparam cfsqltype="cf_sql_TIMESTAM
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
, <cfqueryparam cfsqltype="cf_sql_INTEGER"
)
</cfquery>
<cfquery name="articlemaxcols" datasource="#variables.dsn
select
max(art_ID) as maxart_ID
from
article
</cfquery>
<cfset arguments.articleBean.seta
</cftransaction>
</cffunction>
==========================
Hope this helps some.
}Wulf{
Business Accounts
Answer for Membership
by: jimmy282Posted on 2005-11-20 at 23:30:38ID: 15332130
you can use this query for Mysql
Select LAST_INSERT_ID()
and this will return you the latest inserted ID.