• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Best Practice to capture the Id of a newly inserted recordset (MSQL/CFML)

     I am adding a new ministry into the db with this sql statement:


<cfquery datasource="DSN_Vineyard">
            INSERT INTO ministries (church_id, ministry_desc, ministry_leader
                  <cfif admin1_new is not "">, admin1 </cfif>
                  <cfif admin2_new is not "">, admin2 </cfif>
                  <cfif admin3_new is not "">, admin3 </cfif>
                  <cfif admin4_new is not "">, admin4 </cfif>
                  <cfif admin5_new is not "">, admin5 </cfif>)
            VALUES (#church_id#, '#ministry_desc_new#', #ministry_leader_new#
                  <cfif admin1_new is not "">, #admin1_new# </cfif>
                  <cfif admin2_new is not "">, #admin2_new# </cfif>
                  <cfif admin3_new is not "">, #admin3_new# </cfif>
                  <cfif admin4_new is not "">, #admin4_new# </cfif>
                  <cfif admin5_new is not "">, #admin5_new# </cfif>)
      </cfquery>


I need to be able to capture the id of the newly inserted recordset and add that to a second db collumn titled page_entities which is used to track everything. What is the best practice to capture the id of the newly inserted recordset?
0
btintermedia
Asked:
btintermedia
  • 3
2 Solutions
 
JoeNuvoCommented:
I don't have knowledge about CFML

but for MSSQL, when you insert new data into table, you can obtain the PRIMARY KEY value by function SCOPE_IDENTITY()  or by variable @@IDENTITY (but the first one is recommend)

for ex

INSERT INTO table (field1, field2) VALUES (value1, value2)

SELECT @new_id = SCOPE_IDENTITY()
0
 
JoeNuvoCommented:
Oops,  value to obtain is the identity value, not primary key. (since PRIMARY KEY is not always identity value)
0
 
JoeNuvoCommented:
2 methods

1. using SCOPE_IDENTITY()  

<cfquery datasource="DSN_Vineyard">
            INSERT INTO ministries (church_id, ministry_desc, ministry_leader
                  <cfif admin1_new is not "">, admin1 </cfif>
                  <cfif admin2_new is not "">, admin2 </cfif>
                  <cfif admin3_new is not "">, admin3 </cfif>
                  <cfif admin4_new is not "">, admin4 </cfif>
                  <cfif admin5_new is not "">, admin5 </cfif>)
            VALUES (#church_id#, '#ministry_desc_new#', #ministry_leader_new#
                  <cfif admin1_new is not "">, #admin1_new# </cfif>
                  <cfif admin2_new is not "">, #admin2_new# </cfif>
                  <cfif admin3_new is not "">, #admin3_new# </cfif>
                  <cfif admin4_new is not "">, #admin4_new# </cfif>
                  <cfif admin5_new is not "">, #admin5_new# </cfif>)

          SELECT SCOPE_IDENTITY() as NewID
      </cfquery>

2. using OUTPUT
     assume your identity field name is MinistryID

<cfquery datasource="DSN_Vineyard">
            INSERT INTO ministries (church_id, ministry_desc, ministry_leader
                  <cfif admin1_new is not "">, admin1 </cfif>
                  <cfif admin2_new is not "">, admin2 </cfif>
                  <cfif admin3_new is not "">, admin3 </cfif>
                  <cfif admin4_new is not "">, admin4 </cfif>
                  <cfif admin5_new is not "">, admin5 </cfif>)
             OUTPUT INSERTED.MinistryID as [NewID]
            VALUES (#church_id#, '#ministry_desc_new#', #ministry_leader_new#
                  <cfif admin1_new is not "">, #admin1_new# </cfif>
                  <cfif admin2_new is not "">, #admin2_new# </cfif>
                  <cfif admin3_new is not "">, #admin3_new# </cfif>
                  <cfif admin4_new is not "">, #admin4_new# </cfif>
                  <cfif admin5_new is not "">, #admin5_new# </cfif>)
      </cfquery>

0
 
erikTsomikSystem Architect, CF programmer Commented:
you can do this
<cfquery datasource="DSN_Vineyard" result="qryResult">
            INSERT INTO ministries (church_id, ministry_desc, ministry_leader
                  <cfif admin1_new is not "">, admin1 </cfif>
                  <cfif admin2_new is not "">, admin2 </cfif>
                  <cfif admin3_new is not "">, admin3 </cfif>
                  <cfif admin4_new is not "">, admin4 </cfif>
                  <cfif admin5_new is not "">, admin5 </cfif>)
            VALUES (#church_id#, '#ministry_desc_new#', #ministry_leader_new#
                  <cfif admin1_new is not "">, #admin1_new# </cfif>
                  <cfif admin2_new is not "">, #admin2_new# </cfif>
                  <cfif admin3_new is not "">, #admin3_new# </cfif>
                  <cfif admin4_new is not "">, #admin4_new# </cfif>
                  <cfif admin5_new is not "">, #admin5_new# </cfif>)
      </cfquery>

<cfset newID = qryResult..IDENTITYCOL>
0
 
_agx_Commented:
>> VALUES (#church_id#, '#ministry_desc_new#', #ministry_leader_new#

As an aside, always use cfqueryparam with MS SQL. Because that query is highly vulnerable to sql injection.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now