If I execute the following SQL statement through ASP (as one string sent through to the Execute statement of a connection object), am I guaranteed to get the identity column of the record that was just inserted, or could two users get the same ID if they are both executing the statement at the same time? SET NOCOUNT ON INSERT INTO [X] (Y) VALUES ('z') SELECT @@IDENTITY AS NewID SET NOCOUNT OFF
I should add that there are no triggers or anything fancy like that. [quoted text, click to view] brian.luk...@gmail.com wrote: > If I execute the following SQL statement through ASP (as one string > sent through to the Execute statement of a connection object), am I > guaranteed to get the identity column of the record that was just > inserted, or could two users get the same ID if they are both executing > the statement at the same time? > > SET NOCOUNT ON > INSERT INTO [X] (Y) VALUES ('z') > SELECT @@IDENTITY AS NewID > SET NOCOUNT OFF
[quoted text, click to view] > If I execute the following SQL statement through ASP (as one string > sent through to the Execute statement of a connection object),
Why are you doing this? First, use a stored procedure, second, use SCOPE_IDENTITY (I assume SQL Server 2000 or better). And I don't know what the point of "SET NOCOUNT OFF" is in this case. CREATE PROCEDURE dbo.AddX @y CHAR(1) AS BEGIN SET NOCOUNT ON; INSERT dbo.X ( Y ) SELECT @y; SELECT [NewID] = SCOPE_IDENTITY(); END GO Better yet, use an OUTPUT paremeter to send the IDENTITY value back to ASP, much more efficient than using a resultset (though a little more complex to code). Check out some posts by Bob Barrows, he has several where he walks through using a Command Object in ASP, and I bet he'll point you to his code that helps automate the generation of ASP code to talk with a stored procedure... A
Hi Aaron, This is from some old code--I just want to check that there is no issue with concurrent users getting the same ID here. Brian [quoted text, click to view] Aaron Bertrand [SQL Server MVP] wrote: > > If I execute the following SQL statement through ASP (as one string > > sent through to the Execute statement of a connection object), > > Why are you doing this? > > First, use a stored procedure, second, use SCOPE_IDENTITY (I assume SQL > Server 2000 or better). And I don't know what the point of "SET NOCOUNT > OFF" is in this case. > > CREATE PROCEDURE dbo.AddX > @y CHAR(1) > AS > BEGIN > > SET NOCOUNT ON; > > INSERT dbo.X > ( > Y > ) > SELECT @y; > > SELECT [NewID] = SCOPE_IDENTITY(); > > END > GO > > > Better yet, use an OUTPUT paremeter to send the IDENTITY value back to ASP, > much more efficient than using a resultset (though a little more complex to > code). > > Check out some posts by Bob Barrows, he has several where he walks through > using a Command Object in ASP, and I bet he'll point you to his code that > helps automate the generation of ASP code to talk with a stored procedure... > > A
[quoted text, click to view] brian.lukoff@gmail.com wrote: > If I execute the following SQL statement through ASP (as one string > sent through to the Execute statement of a connection object), am I > guaranteed to get the identity column of the record that was just > inserted, or could two users get the same ID if they are both > executing the statement at the same time? > > SET NOCOUNT ON > INSERT INTO [X] (Y) VALUES ('z') > SELECT @@IDENTITY AS NewID > SET NOCOUNT OFF
No. However, in certain cases, they could get the wrong ID. If the table into which you are inserting has a trigger that inserts data into a table containing an identity column, then @@IDENTITY will return the ID generated by the triggered insert, not the triggering insert. These flaws that were addressed by the introduction of the IDENT_CURRENT and SCOPE_IDENTITY functions in SQL 2000. I suggest you look these up either in SQL Books Online if you have them installed on your machine ( http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp) or search msdn.microsoft.com/library Oh, I just read your followup in which you state you have no triggers, so @@IDENTITY should be safe. As Aaron stated, my preference is to use a stored procedure with an output parameter to return the ID value. Here is the link he talked about: http://groups.google.com/group/microsoft.public.inetserver.asp.general/browse_frm/thread/86b547a5c7714ddf/757cd0264768493c?lnk=st&q=stored+procedure+parameters+generator&rnum=1&hl=en#757cd0264768493c -- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM"
OK, I will look into switching to SCOPE_IDENTITY for the future, but I can be sure that @@IDENTITY didn't cause multiple users to get the same ID? (There are no triggers created by default, right; I'd have to create them using a CREATE TRIGGER statement?) I'm trying to understand how @@IDENTITY works (at least in the simple situation where there are no triggers). I have heard that it is a "global" variable. Does that mean that it is "global" over the entire session of a particular user, but that it is maintained separately for each user (i.e., each user accessing the ASP page from a separate machine)? Brian [quoted text, click to view] Bob Barrows [MVP] wrote: > Aaron Bertrand [SQL Server MVP] wrote: > >> Oh, I just read your followup in which you state you have no > >> triggers, so @@IDENTITY should be safe. > > > > I still think SCOPE_IDENTITY is safeR and better practice (who knows > > what kind of trigger(s) will be added to the table tomorrow, or next > > week, or next year...). > > I can't argue with that. I should have added " ... but you should switch to > SCOPE_IDENTITY" to that statement. > -- > Microsoft MVP - ASP/ASP.NET > Please reply to the newsgroup. This email account is my spam trap so I > don't check it very often. If you must reply off-line, then remove the > "NO SPAM"
[quoted text, click to view] > This is from some old code--I just want to check that there is no issue > with concurrent users getting the same ID here.
Nope, different sessions cannot simultaneously generate the same IDENTITY value, but it could be wrong (which Bob is getting into, and why I suggested to use SCOPE_IDENTITY() instead of @@IDENTITY).
[quoted text, click to view] > Oh, I just read your followup in which you state you have no triggers, so > @@IDENTITY should be safe.
I still think SCOPE_IDENTITY is safeR and better practice (who knows what kind of trigger(s) will be added to the table tomorrow, or next week, or next year...).
[quoted text, click to view] Aaron Bertrand [SQL Server MVP] wrote: >> Oh, I just read your followup in which you state you have no >> triggers, so @@IDENTITY should be safe. > > I still think SCOPE_IDENTITY is safeR and better practice (who knows > what kind of trigger(s) will be added to the table tomorrow, or next > week, or next year...).
I can't argue with that. I should have added " ... but you should switch to SCOPE_IDENTITY" to that statement. -- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM"
Thanks for your help and for the reference! Brian [quoted text, click to view] Bob Barrows [MVP] wrote: > brian.lukoff@gmail.com wrote: > > OK, I will look into switching to SCOPE_IDENTITY for the future, but I > > can be sure that @@IDENTITY didn't cause multiple users to get the > > same > > ID? > > I believe we've already answered this ... but, yes you can be sure of that. > > > (There are no triggers created by default, right; I'd have to > > create them using a CREATE TRIGGER statement?) > > Correct > > > > I'm trying to understand how @@IDENTITY works (at least in the simple > > situation where there are no triggers). I have heard that it is a > > "global" variable. Does that mean that it is "global" over the entire > > session of a particular user, but that it is maintained separately for > > each user (i.e., each user accessing the ASP page from a separate > > machine)? > > > Close enough. > > Do you SQL Books Online (BOL) installed? If not, there is no better online > reference for any MS product, so go get them: > http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp > > > > -- > Microsoft MVP - ASP/ASP.NET > Please reply to the newsgroup. This email account is my spam trap so I > don't check it very often. If you must reply off-line, then remove the > "NO SPAM"
[quoted text, click to view] brian.lukoff@gmail.com wrote: > OK, I will look into switching to SCOPE_IDENTITY for the future, but I > can be sure that @@IDENTITY didn't cause multiple users to get the > same > ID?
I believe we've already answered this ... but, yes you can be sure of that. [quoted text, click to view] > (There are no triggers created by default, right; I'd have to > create them using a CREATE TRIGGER statement?)
Correct [quoted text, click to view] > > I'm trying to understand how @@IDENTITY works (at least in the simple > situation where there are no triggers). I have heard that it is a > "global" variable. Does that mean that it is "global" over the entire > session of a particular user, but that it is maintained separately for > each user (i.e., each user accessing the ASP page from a separate > machine)? >
Close enough. Do you SQL Books Online (BOL) installed? If not, there is no better online reference for any MS product, so go get them: http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp -- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM"
Don't see what you're looking for? Try a search.
|