Solved

SCOPE_IDENTITY

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

22 Experts available now in Live!

Get 1:1 Help Now