User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ColdFusion section within the Web Development category of DaniWeb, a massive community of 427,937 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,960 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ColdFusion advertiser: Programming Forums
Views: 1450 | Replies: 3 | Solved
Reply
Join Date: Mar 2008
Posts: 5
Reputation: Hercf is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Hercf Hercf is offline Offline
Newbie Poster

Insert query.

  #1  
Jun 15th, 2008
Hi everybody!
Could you teach me how to get back the “primary key”: (“id”), from an “insert query”?

*
  1. <cffunction name="insertData" access="public">
  2. <cfargument name="formData" type="struct" required="yes">
  3.  
  4. <cfquery name="qInsert" datasource="mydata">
  5. INSERT INTO mytable
  6. (email, firstname, name)
  7. VALUES
  8. ('#formData.email#', '#formData.firstname#', '#formData.name#')
  9. </cfquery>
  10. </cffunction>
*
How could I get back the primary key:”id” and send it to another page? (MySQL)
Thanks a lot!
Last edited by peter_budo : Jun 16th, 2008 at 9:06 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 70
Reputation: cmhampton is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Insert query.

  #2  
Jun 23rd, 2008
Use SCOPE_IDENTITY(). It will return the id of the record just created, provided the field is set as the identity column.

  1. <cffunction name="insertData" access="public">
  2. <cfargument name="formData" type="struct" required="yes">
  3.  
  4. <cfquery name="qInsert" datasource="mydata">
  5. INSERT INTO mytable
  6. (email, firstname, name)
  7. VALUES
  8. ('#formData.email#', '#formData.firstname#', '#formData.name#')
  9.  
  10. SELECT id = SCOPE_IDENTITY()
  11. </cfquery>
  12.  
  13. <cfreturn qInsert.id />
  14.  
  15. </cffunction>
Reply With Quote  
Join Date: Mar 2008
Posts: 5
Reputation: Hercf is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Hercf Hercf is offline Offline
Newbie Poster

Re: Insert query.

  #3  
Jun 29th, 2008
Thank you, cmhampton !
How about cfquery: "result" ?What you prefer?
H.


Originally Posted by cmhampton View Post
Use SCOPE_IDENTITY(). It will return the id of the record just created, provided the field is set as the identity column.

  1. <cffunction name="insertData" access="public">
  2. <cfargument name="formData" type="struct" required="yes">
  3.  
  4. <cfquery name="qInsert" datasource="mydata">
  5. INSERT INTO mytable
  6. (email, firstname, name)
  7. VALUES
  8. ('#formData.email#', '#formData.firstname#', '#formData.name#')
  9.  
  10. SELECT id = SCOPE_IDENTITY()
  11. </cfquery>
  12.  
  13. <cfreturn qInsert.id />
  14.  
  15. </cffunction>
Reply With Quote  
Join Date: Feb 2008
Posts: 70
Reputation: cmhampton is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Insert query.

  #4  
Jun 29th, 2008
Originally Posted by Hercf View Post
Thank you, cmhampton !
How about cfquery: "result" ?What you prefer?
H.


I'm not sure I understand your question.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ColdFusion Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ColdFusion Forum

All times are GMT -4. The time now is 6:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC