Not yet rated

Problem

There are many times where we want the use the ID of a newly inserted record in the next line of code proceeding an insert on a table with an auto increment field. In CF8 this was available but the result was different depending on the database type. In ColdFusion 9 Adobe solved this issue but using a generic result GENERATEDKEY. All existing results still work in CF9 so existing code wont break but going forward this is a much better way to work and makes your ColdFusion code less database dependent.

Solution

Add the result attribute to the < cfquery > block to access the generatedKey

Detailed explanation


<cfquery name="insertQuery" result="insertQueryResult"> 
  insert 
  into       testData 
                ( data ) 
  values  ( <cfqueryparam cfsqltype="cf_sql_varchar" value="Test String" /> )
</cfquery>

 

<cfdump var="#insertQueryResult.generatedKey#" />

 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes