Solved

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

Posted on 2004-09-29
5
481 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
  • 3
5 Comments
 
LVL 4

Assisted Solution

by:reggi635
reggi635 earned 250 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 250 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 250 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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