Insert query.

Thread Solved

Join Date: Mar 2008
Posts: 5
Reputation: Hercf is an unknown quantity at this point 
Solved Threads: 0
Hercf Hercf is offline Offline
Newbie Poster

Insert query.

 
0
  #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 10:06 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 72
Reputation: cmhampton is an unknown quantity at this point 
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Insert query.

 
0
  #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 Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: Hercf is an unknown quantity at this point 
Solved Threads: 0
Hercf Hercf is offline Offline
Newbie Poster

Re: Insert query.

 
0
  #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 Quick reply to this message  
Join Date: Feb 2008
Posts: 72
Reputation: cmhampton is an unknown quantity at this point 
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Insert query.

 
0
  #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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC