Skip to main content

Posts

Showing posts from October, 2007

Using SQL's Scope_Identity

So you want to get the ID(PrimaryKey) of the last record inserted into the database without having to do a second cfquery block. Here is an example of doing it the hard way. <cfset value1 = 2007> <cfset myuuid = CreateUUID()> <cfquery name="insert" datasource="#APPLICATION.DSN.DB#">     INSERT INTO table(column1,column2)     VALUES(#VARIABLES.value1#, '#VARIABLES.myUUID#') </cfquery> <cfquery name="select" datasource="#APPLICATION.DSN.DB#">     SELECT ID     FROM table     WHERE column2=<cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.myUUID#"/> </cfquery> You would then reference the ID as select.ID. The easier way. <cfquery name="insert" datasource="#APPLICATION.DSN.DB#">     INSERT INTO table(column1,column2)     VALUES(#VARIABLES.value1#, '#VARIABLES.myUUID#')     SELECT Scope_Identity() AS ID </cfquery> The ID returned woul