[dba-SQLServer]@@IDENTITY and SCOPE_IDENTITY

Francisco H Tapia my.lists at verizon.net
Wed Mar 19 11:54:34 CST 2003


BOL: INDEX KEYWORD @@IDENTITY

:What's not so clear is multiple users inserting one record at a time.
1) BOL: @@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.

That is to say current session = userA.  UserB is in his OWN session so you
could have Users A, B, C, D, E, F... insert records before user A can run
the @@Idenity statement and UserA will still get their own Id back from
their own session, not that of userZ.

2)YES



-Francisco
http://rcm.netfirms.com

On Tuesday, March 18, 2003 6:26 PM [GMT-8],
Susan Harkins <harkins at iglou.com> wrote:

: I have two questions about these two functions.
:
: 1.) BOL is really clear on inserting more than one record or triggers
: -- I understand the scope issues in those situations. What's not so
: clear is multiple users inserting one record at a time. If a stored
: procedure inserts a record and then grabs the newly inserted identity
: value using @@IDENTITY, is there any chance @@IDENTITY might return a
: value other than the right one? Say a number of users are updating
: the same table at the same time. UserA inserts identity value x but
: before the stored procedure can grab x, UserB inserts identity value
: y. Is there any possibility @@IDENTITY will return y to UserA instead
: of x? Or, does SQL Server resolve these conflicts internally?
:
: 2.) A single sp inserts two records. In this case, @@IDENTITY and
: SCOPE_IDENTITY return the same value -- always the last inserted
: value. Right?
:





More information about the dba-SQLServer mailing list