Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Best solution to SQL insert-retrieve query function. (Tacobell777 - but others too!)

Posted on 2004-09-29
5
Medium Priority
?
485 Views
Last Modified: 2013-12-24
Been looking forever for a better solution to this than wrapping two queries into a transaction in order to retrieve the new record ID of a row that was just inserted by the query.  Don't want to have to write stored procs for every query I use and hate having to hit SQL twice to just get my new records auto-increment ID value.  This is primarily for TacoBell777, but if anyone else has any really nifty <CFQUERY> tricks or tips here I'll possibly split some points their way too!

BTW - for easy searching by others and for my own referrence, TacoBell777 if you could put a complete example of a short insert-retrieve function here in addition to your other example I'd love it!  Lets say that table TBLMYDATA has the following columns:
RowID (INT, AUTO, PK)
TextCol (NVARCHAR)
NumCol (INT)
BitCol (BIT)

Thanks again for the cool tip and for understanding SQL much better than I do. :)
0
Comment
Question by:digicidal
[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 4

Assisted Solution

by:reggi635
reggi635 earned 1000 total points
ID: 12187008
No offenses tacobell. Watch out dude you already have some fans in here :-)

My Try :

<cfquery name="qry" datasource="dsn">
 INSERT INTO TBLMYDATA (TextCol, NumCol, BitCol)
      VALUES
      <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#TextCol#">,
      <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#NumCol#">,
      <cfqueryparam cfsqltype="CF_SQL_BIT" value="#BitCol#">
SELECT @@identity AS newID
</cfquery>

<cfoutput>#qry.newID#</cfoutput>

might wanna also check the scope_identity in case u have a trigger.

REGGI
0
 
LVL 9

Author Comment

by:digicidal
ID: 12187579
Hmmm... I was under the (apparently mistaken) belief that you could only guarantee that @@identity would return the correct ID when run in a storedproc - since @@identity is a global and since SQL is a multithreaded app as is CF that you might possibly have someone do an insert on the server in the brief space between the INSERT and the SELECT statements.  In which case the @@Identity would actually return the recordID of the second users INSERT and not the first.

Is this incorrect?  Tacobells solution is the following:

<cfquery ...>
DECLARE @pk INT

INSERT statement here

SET @pk = SCOPE_IDENTITY()

SELECT @pk AS primaryKey
</cfquery>

You actually referrenced that is the last part of your comment... can you (or someone else for that matter) provide evidence or links to conclusively support one method or the other in most cases (I realize that other steps may need to be taken in the case of really complex transactions or triggers on the server side).  You've earned some points already reggi635 - and if Tacobell doesn't choose to comment here I'll award you instead with the whole thing.  I'm really just looking for a discussion on which method is truly better and why.  Particularly in a MS SQL CF MX situation.  I'm just convinced that my old method (transactions with a select MAX(ID) is definitely NOT the best, but I would love to hear some evidence as to why a SCOPE_IDENTITY() method is preferrable or not to @@Identity.
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 12187640
yes you are better of using SCOPE_IDENTITY()

you guys are robbing my points!!!! ;-))
0
 
LVL 17

Accepted Solution

by:
Tacobell777 earned 1000 total points
ID: 12187672
oops, I have to stop just glancing over the questions and actually read them...

<cfquery ...>
INSERT ...

SELECT SCOPE_IDENTITY() AS pkID
</cfquery>

I ussually select it into a variable because I do more than one thing in the same cfquery...

following taken from the online books...

@@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

SCOPE_IDENTITY()
Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch. Thus, two statements are in the same scope if they are in the same stored procedure, function, or batch.
0
 
LVL 17

Assisted Solution

by:Tacobell777
Tacobell777 earned 1000 total points
ID: 12187683
PS. MAX is used because it works in more than one database, @@identity and scope_identity() are database specific..
The impact of using it is really not that big... however it is more error prone...

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

704 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