CFLib.org – Common Function Library Project

getGeneratedKey(resultStruct)

Last updated February 15, 2008

Version: 1 | Requires: ColdFusion 8 | Library: DatabaseLib

 
Rated 2 time(s). Average Rating: 5.0

Description:
This UDF takes the result struct from cfquery and returns the proper generated key from that struct.

Return Values:
Returns a value.

Example:

view plain print about
<cfquery name="insertArtist" datasource="cfartgallery" result="r">
insert into artists
(firstName, lastName)
values(
'todd','sharp'
)
</cfquery>
<cfquery name="getArtists" datasource="cfartgallery">
select *
from artists
</cfquery>
<cfdump var="#getArtists#">
<cfoutput>#getGeneratedKey(r)#</cfoutput>

Parameters:

Name Description Required
resultStruct Structure. Yes

Full UDF Source:

view plain print about
<!---
 Normalizes the various possible returned keys in the cfquery result struct.
 
 @param resultStruct      Structure. (Required)
 @return Returns a value. 
 @author Todd Sharp (todd@cfsilence.com) 
 @version 1, October 14, 2008 
--->

<cffunction name="getGeneratedKey" hint="i normalize the key returned from cfquery" output="false">
    <cfargument name="resultStruct" hint="the result struct returned from cfquery" />
    <cfif structKeyExists(arguments.resultStruct, "IDENTITYCOL")>
        <cfreturn arguments.resultStruct.IDENTITYCOL />
    <cfelseif structKeyExists(arguments.resultStruct, "ROWID")>
        <cfreturn arguments.resultStruct.ROWID />
    <cfelseif structKeyExists(arguments.resultStruct, "SYB_IDENTITY")>
        <cfreturn arguments.resultStruct.SYB_IDENTITY />
    <cfelseif structKeyExists(arguments.resultStruct, "SERIAL_COL")>
        <cfreturn arguments.resultStruct.SERIAL_COL />    
    <cfelseif structKeyExists(arguments.resultStruct, "GENERATED_KEY")>
        <cfreturn arguments.resultStruct.GENERATED_KEY />
    <cfelse>
        <cfreturn />
    </cfif>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson