943,871 Members | Top Members by Rank

Ad:
  • ColdFusion Discussion Thread
  • Marked Solved
  • Views: 4997
  • ColdFusion RSS
Jun 15th, 2008
0

Insert query.

Expand Post »
Hi everybody!
Could you teach me how to get back the “primary key”: (“id”), from an “insert query”?

*
coldfusion Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hercf is offline Offline
5 posts
since Mar 2008
Jun 23rd, 2008
0

Re: Insert query.

Use SCOPE_IDENTITY(). It will return the id of the record just created, provided the field is set as the identity column.

ColdFusion Syntax (Toggle Plain Text)
  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>
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Jun 29th, 2008
0

Re: Insert query.

Thank you, cmhampton !
How about cfquery: "result" ?What you prefer?
H.


Click to Expand / Collapse  Quote originally posted by cmhampton ...
Use SCOPE_IDENTITY(). It will return the id of the record just created, provided the field is set as the identity column.

ColdFusion Syntax (Toggle Plain Text)
  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>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hercf is offline Offline
5 posts
since Mar 2008
Jun 29th, 2008
0

Re: Insert query.

Click to Expand / Collapse  Quote originally posted by Hercf ...
Thank you, cmhampton !
How about cfquery: "result" ?What you prefer?
H.
I'm not sure I understand your question.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ColdFusion Forum Timeline: title tag
Next Thread in ColdFusion Forum Timeline: <cfmail> question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC