Groups | Blog | Home
all groups > inetserver asp db > june 2006 >

inetserver asp db : @@IDENTITY in SQL server


brian.lukoff NO[at]SPAM gmail.com
6/16/2006 3:12:13 PM
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
brian.lukoff NO[at]SPAM gmail.com
6/16/2006 3:17:48 PM
I should add that there are no triggers or anything fancy like that.

[quoted text, click to view]
Aaron Bertrand [SQL Server MVP]
6/16/2006 6:37:23 PM
[quoted text, click to view]

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

brian.lukoff NO[at]SPAM gmail.com
6/16/2006 8:33:16 PM
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]
Bob Barrows [MVP]
6/17/2006 9:34:47 AM
[quoted text, click to view]

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"

brian.lukoff NO[at]SPAM gmail.com
6/18/2006 8:06:10 AM
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]
Aaron Bertrand [SQL Server MVP]
6/18/2006 8:29:00 AM
[quoted text, click to view]

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).

Aaron Bertrand [SQL Server MVP]
6/18/2006 8:55:03 AM
[quoted text, click to view]

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...).

Bob Barrows [MVP]
6/18/2006 9:34:45 AM
[quoted text, click to view]

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"

brian.lukoff NO[at]SPAM gmail.com
6/18/2006 11:49:24 AM
Thanks for your help and for the reference!

Brian

[quoted text, click to view]
Bob Barrows [MVP]
6/18/2006 12:01:14 PM
[quoted text, click to view]

I believe we've already answered this ... but, yes you can be sure of that.

[quoted text, click to view]

Correct
[quoted text, click to view]
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"

AddThis Social Bookmark Button