Monday, August 3, 2009

ColdFusion - Getting ID of inserted row

How to get ID of row inserted using CFQUERY:

MySQL:
<cfquery result="insertrow" datasource="#application.ds#">
INSERT INTO [table]([column1], [column2]...)
VALUES([value1], [value2]...)
</cfquery>
ID: insertrow.generated_key


T-SQL
<cfquery name="insertrow" datasource="#application.ds#">
INSERT INTO [table]([column1], [column2]...)
VALUES([value1], [value2]...)

SELECT @@IDENTITY AS id
</cfquery>
ID: insertrow.id


Notice that when using MySQL, you use result, while when using T-SQL you use name.

No comments:

Post a Comment