Solved

SCOPE_IDENTITY

Posted on 2006-07-17
2
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 65
Coding for the first time 9 69
Updating a single record in forEach using C#/Entity 7 26
aspx ascx, c# 7 18
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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…

733 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