Solved

SCOPE_IDENTITY

Posted on 2006-07-17
2
386 Views
Last Modified: 2008-03-17
I have a question about MS SQL Servers SCOPE_IDENTITY  -   this is the explanation MSDN gives:

SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

Can someone provide a brief explanation on what current session and current scope means?

I just want to be **guaranteed** that I will get the right value if I say:

SqlCommand comm ....

comm.CommandText = "INSERT INTO ..... ; SELECT SCOPE_IDENTITY()"
......

even if there are other connections to this DB going on in other currently running applications.
0
Comment
Question by:ARACK04
2 Comments
 
LVL 10

Accepted Solution

by:
athapa earned 250 total points
ID: 17127294
If you have multiple applications (or instance or thread) which are inserting data into your server then you should use Scope Identity. If you just use identity then it may pick up the last identity value inserted into the database. However scope identity will return the last identity value generated by your session so it will be guarented to return you the last one you inserted.

AT
0
 
LVL 13

Assisted Solution

by:BlackTigerX
BlackTigerX earned 250 total points
ID: 17128004
"even if there are other connections to this DB going on in other currently running applications"
yes, you will get the right value using scope_identity()
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 36
Vb.net dynamic formulas in runtime 11 61
Name space syntax error 12 41
Error when loading the database 16 56
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now