If you are using SQL Server, you can also use the @@IDENTITY function in SQL Server and wrap the statement in the same query as the insert statement. @@IDENTITY returns the very last ID that was inserted into the database,regardless of table.
For example:
<CFQUERY NAME="myQuery" DATASOURCE="myDataSource">
INSERT INTO Table
(Field1,
Field2,
Field3)
VALUES
('Field1',
'Field2',
'Field3')
SELECT @@IDENTITY AS RecordID
</CFQUERY>
<CFDUMP VAR="#myQuery.RecordID#">
This will insert the record and return its ID as a single process.
Main Topics
Browse All Topics





by: pinaldavePosted on 2005-06-23 at 16:03:15ID: 14289831
hi, you can always use lock around your insert and selection like this and it will give you last isserted record for sure.
<cflock name="insertrecord" timeout="10">
<cfquery datasource="mydsn">
INSERT INTO mytable (yourfields) VALUES (#yourvalues#)
</cfquery>
<cfquery datasource="mydsn" name="getnewID">
SELECT MAX(RecordID) AS NewID FROM mytable
</cfquery>
</cflock>
This way that will always select the last inserted value.
Regards,
---Pinal