Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using SCOPE_IDENTITY in coldfusion...

Status
Not open for further replies.

fdgsogc

Vendor
Feb 26, 2004
160
CA
I'm trying to identify the ID of the column I just inserted in my MSSQL table. I've learned that I need to use SCOPE_IDENTITY function. The MSSQL server forums tell me how to use this in a stored procedure.

I would like to use it at the end of my insert query. I just can't find the proper syntax to do it.

Any ideas?
 
I'm not sure if you can, I've never tried it. As a general rule, I try to always use Stored Procedures anyway. You could put your insert statement in a Procedure and just have the Procedure return the value to CF.



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
Here's what I finally ended up doing. The important parts are in red.
Code:
<cfquery name="InsertServiceData" datasource="#data#">
[red]SET NOCOUNT ON[/red]
Insert into sch_cust_servicedata
(clientid, 
contactid, 
eventid, 
AssetID,
DeliveryMethod,
PickupStreet,PickupCity,PickupState,PickUpPostalCode,
)
values
('#clientid#',
'#contactid#',
'#eventid#',
'#SelectYourBoat#',
'#SelectDeliveryMethod#',
'#PickupStreet#','#PickupCity#','#PickupState#','#PickUpPostalCode#',
)
[red]SELECT SCOPE_IDENTITY() AS [lastid];
SET NOCOUNT OFF[/red]
</cfquery>

[red]<cfoutput><cfset lastid = "#insertservicedata.lastid#"></cfoutput>[/red]
 
I actually never use semi-colons. It existed in the code I copied from another site. The code works as posted.
 
Sorry about the dangling commas. I was shortening my query for the sake of posting less text. Forgot to trim those commas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top