Solved

SCOPE_IDENTITY

Posted on 2006-07-17
2
391 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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