MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 13th, 2004, 02:44 PM
Gnosis Gnosis is offline
Sellout
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 205 Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 10 h 14 m 58 sec
Reputation Power: 6
Safely select what I just inserted?

I was thinking that this question probably comes up a lot...but my forum searches didn't turn anything up, so here goes:

I have an id field that auto increments (identity) for each record.
I insert a record.
How can I safely extract the id field of the record for use in another query, avoiding the possiblity that another record is inserted before I can get at it?

Thanks,
G

Reply With Quote
  #2  
Old July 13th, 2004, 03:11 PM
Gnosis Gnosis is offline
Sellout
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 205 Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 10 h 14 m 58 sec
Reputation Power: 6
I forgot to mention I have to do this through coldFusion. For now I'm using
Code:
<cftransaction>
   <cfquery datasource="dsn">
     INSERT INTO table([columns])
     VALUES([values])
   </cfquery>
   <cfquery datasource="dsn" name="newId">
     SELECT @@IDENTITY AS newId
   </cfquery>
</cftransaction>

which seems to make sense. Let me know if there's a better way!

Reply With Quote
  #3  
Old July 13th, 2004, 03:45 PM
lucasalexander lucasalexander is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Atlanta
Posts: 39 lucasalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via AIM to lucasalexander Send a message via MSN to lucasalexander
Use a stored procedure to do the insert and return the identity value.

Lucas Alexander
http://www.alexanderdevelopment.net

Reply With Quote
  #4  
Old July 14th, 2004, 12:10 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,717 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 31 m 7 sec
Reputation Power: 986
well, using a transaction lock around INSERT and SELECT @@IDENTITY is one method

calling a stored procedure is another method

here's a third: use two queries, one for the INSERT and then another for the SELECT, except in the SELECT what you're doing is querying the row based on the values of the other field(s) which identify the row (technically called a candidate or alternate key)

every table that uses a surrogate key has somewhere lurking within it a column or set of columns that uniquely identifies the row

(if it doesn't, some people will argue that you have a substantial design problem)

so just query the row back using the values of those fields, which will still be sitting there in your CF variables

and the best part: no transaction lock required
__________________
r937.com | rudy.ca

Reply With Quote
  #5  
Old July 14th, 2004, 12:23 AM
Gnosis Gnosis is offline
Sellout
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 205 Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 10 h 14 m 58 sec
Reputation Power: 6
That's true. I've done just that in the past, but I always had this fear in the back of my mind...what if? what if somehow another record with the same set just happens to get in there? I want it to be utterly impossible!

In this case you're right though. There's a user id in the row, so it would be damn difficult for a race condition to occur.

I suppose a user could give his login to someone else and they could both click at the same time....!

Reply With Quote
  #6  
Old July 14th, 2004, 12:34 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,717 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 31 m 7 sec
Reputation Power: 986
"another record with the same set" is the crux of the matter

have a look at the table design and find the "real" key

(it isn't the primary key, because if you're using an identity column, chances are you've declared that as the primary key)

this "real" key should have a unique constraint declared on it

if you weren't using an identity column, what would be the primary key?

make sure it has a unique constraint

then the "same set" can never happen

Reply With Quote
  #7  
Old July 14th, 2004, 10:56 AM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,173 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 24
One caveat...

@@IDENTITY returns the last IDENTITY produced on a connection regardless of which table produced the IDENTITY value. So if you have a trigger on a table that creates an IDENTITY value, you would get the IDENTITY of the trigger table, not the primary table.

If you want to be safe and you're using SQL 2000, you might want to consider using SCOPE_IDENTITY() instead of @@IDENTITY. It wil return the IDENTITY of the insert within the scope of the inserts your program submits. Incidental inserts outside of that scope (triggers) are ignored.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Safely select what I just inserted?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway