Solved

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

Posted on 2010-08-12
5
524 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
  • 3
5 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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_
Comment Utility
>> 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now