473,729 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is @@IDENTITY really unique in multi-sessions scope

I have a table EugeneTest(id_n um, fname, minit, lname)
where field "id_num" is type IDENTITY and has UNIQUE constraint

Let's say 2 user execute this code at the same time:

DECLARE @return integer
use EugeneTest
INSERT employees
( fname, minit, lname)
VALUES
( 'Eugene3', 'F', 'Josephs')
SET @return = @@IDENTITY

Is is not possible to get duplicated value in id_num column becuase of
UNIQUE
constraint, but is it possible the both user get the same @@IDENTITY
number
( for example, if @@IDENTITY is evaluated and kept somewhere in the
buffer before the data actually get written to the disk )

Thanks, Eugene
Jul 23 '05 #1
5 4009
Eugene wrote:
I have a table EugeneTest(id_n um, fname, minit, lname)
where field "id_num" is type IDENTITY and has UNIQUE constraint

Let's say 2 user execute this code at the same time:

DECLARE @return integer
use EugeneTest
INSERT employees
( fname, minit, lname)
VALUES
( 'Eugene3', 'F', 'Josephs')
SET @return = @@IDENTITY

Is is not possible to get duplicated value in id_num column becuase of UNIQUE
constraint, but is it possible the both user get the same @@IDENTITY
number
( for example, if @@IDENTITY is evaluated and kept somewhere in the
buffer before the data actually get written to the disk )

Thanks, Eugene


The problem with @@IDENTITY is that, whilst it holds the last identity
value inserted into any table, it is modified by ALL user sessions
(i.e. it has global scope). Thus, if 2 concurrent users execute the
code in your example above, it is possible for User1 to actually
return the value generated by User2 if user2 is running a slpit-second
behind User1.

To get around this problem, you should use SCOPE_IDENTITY( ) instead.
This also returns the most recent indetity value inserted into any
table, BUT ONLY FOR THAT SESSION (i.e. local scope, hence the name).
This means that you will not pick up an identity value generated by
another user session.

Jul 23 '05 #2
On 4 Feb 2005 09:23:17 -0800, "Phil" <ph********@bto penworld.com> wrote:
Eugene wrote:
I have a table EugeneTest(id_n um, fname, minit, lname)
where field "id_num" is type IDENTITY and has UNIQUE constraint

Let's say 2 user execute this code at the same time:

DECLARE @return integer
use EugeneTest
INSERT employees
( fname, minit, lname)
VALUES
( 'Eugene3', 'F', 'Josephs')
SET @return = @@IDENTITY

Is is not possible to get duplicated value in id_num column becuaseof
UNIQUE
constraint, but is it possible the both user get the same @@IDENTITY
number
( for example, if @@IDENTITY is evaluated and kept somewhere in the
buffer before the data actually get written to the disk )

Thanks, Eugene


The problem with @@IDENTITY is that, whilst it holds the last identity
value inserted into any table, it is modified by ALL user sessions
(i.e. it has global scope). Thus, if 2 concurrent users execute the
code in your example above, it is possible for User1 to actually
return the value generated by User2 if user2 is running a slpit-second
behind User1.


Right on 1 - wrong on 2. @@IDENTITY is only affected by statements executed
in the same connection, even though @@ normally means Global.

From the T-SQL Help: "@@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."

To get around this problem, you should use SCOPE_IDENTITY( ) instead.
This also returns the most recent indetity value inserted into any
table, BUT ONLY FOR THAT SESSION (i.e. local scope, hence the name).
This means that you will not pick up an identity value generated by
another user session.


@@IDENTITY won't pick up a value generated by another user session, but it can
pick up a value generated by a trigger action on another table that was
triggered by the action you explicitly executed.
Jul 23 '05 #3
On 4 Feb 2005 09:23:17 -0800, "Phil" <ph********@bto penworld.com> wrote:
Eugene wrote:
I have a table EugeneTest(id_n um, fname, minit, lname)
where field "id_num" is type IDENTITY and has UNIQUE constraint

Let's say 2 user execute this code at the same time:

DECLARE @return integer
use EugeneTest
INSERT employees
( fname, minit, lname)
VALUES
( 'Eugene3', 'F', 'Josephs')
SET @return = @@IDENTITY

Is is not possible to get duplicated value in id_num column becuase

of
UNIQUE
constraint, but is it possible the both user get the same @@IDENTITY
number
( for example, if @@IDENTITY is evaluated and kept somewhere in the
buffer before the data actually get written to the disk )

Thanks, Eugene


As I understand it, the next value for an identity column is incremented
immediately, and not rolled back in order to prevent just that problem. Thus,
IDENTITY values are not guaranteed to be consecutive, they are guaranteed
never to produce duplicates, even if 2 users have insert transactions
overlapping in time.
Jul 23 '05 #4
Looks like I was being sloppy and using session and connection
interchangeably . I must confess I thought I'd finally got the hang of
identity value behaviour - clearly not! :-)

Still, 99% of the time all I need to know is that code at our place
needs to use scope_identity, and not @@identity or ident_current (which
is another topic altogether!)

Jul 23 '05 #5
On 4 Feb 2005 10:03:56 -0800, "Phil" <ph********@bto penworld.com> wrote:
Looks like I was being sloppy and using session and connection
interchangeabl y. I must confess I thought I'd finally got the hang of
identity value behaviour - clearly not! :-)

Still, 99% of the time all I need to know is that code at our place
needs to use scope_identity, and not @@identity or ident_current (which
I agree with that completely :)
is another topic altogether!)

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2629
by: Benny | last post by:
Dear All, Suppose in the program a record is added to a table whose primary key is a identity field. If I really want to get the lastest value for that field after the insertion, is it the best way to use IDENT_CURRENT() to obtain this value? Thanks for your kind attention Yours faithfully,
112
10340
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please, share your experience in using IDENTITY as PK .
4
41596
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I do my insert into 3 different table all using the same uniqueID. I can't use the @@identity function because my application uses a connection pool and it's not garanteed that a connection won't be used
13
1549
by: Jerry Camel | last post by:
I need to be able to generate unique names for files. I was considering that hash alogorithms, but if I had two files with the same name, I'd get the same hash. I am collecting and storing files and I want to store them all in the same directory and I want the names to be cryptic and unique. Any thoughts? Jerry
41
3157
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB) SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15 --get entries just added
3
1584
by: Dave A | last post by:
Has anyone written or using an identity mapper design pattern in an enterprise framework? The identity mapper pattern is described in Martin Fowler's Patterns of Enterprise Application Architecture. It means that subsequent loads of the same object from the database will result in the same object in memory.
1
20987
by: Brad Eck | last post by:
In Access, newID returns a unique for the table. In SQL Server, newid() returns a GUID - unique in the world. I do not need or desire that complexity. Is there a way to get a simple unique int on the table in SQL Server? Brad Eck http://www.sitesdynamic.com http://www.basketsetcetera.com *** Sent via Developersdex http://www.developersdex.com ***
4
4674
by: Dabbler | last post by:
I'm using an SQLCommand to insert row using a text command. Is there a way to return the IDENTITY key value after the insert ? Thanks much!
1
4024
by: cedric.louyot | last post by:
Hi, I've written a schema that looks like : <xs:schema> <xs:complexType name="myType"> <xs:sequence> <xs:element name="e1" type="T1" maxOccurs="unbounded"/> <xs:element name="e2" type="xs:string"/> </xs:sequence>
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.