Solved

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

Posted on 2010-08-12
5
540 Views
Last Modified: 2012-05-10
     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
Comment
Question by:btintermedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 33426781
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
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 33426786
Oops,  value to obtain is the identity value, not primary key. (since PRIMARY KEY is not always identity value)
0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 300 total points
ID: 33426811
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
 
LVL 19

Assisted Solution

by:erikTsomik
erikTsomik earned 200 total points
ID: 33426814
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33426968
>> 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question