C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Difference between IDENT_CURRENT,SCOPE_IDENTITY and @@IDENTITY


Posted Date: 19 Jul 2007    Resource Type: Articles    Category: Databases
Author: KamalMember Level: Gold    
Rating: 1 out of 5Points: 10



SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions in that they return values inserted into IDENTITY columns.

IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope.

SCOPE_IDENTITY and @@IDENTITY will return last identity values generated in any table in the current session.

However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.



Responses

Author: bharat kumar    29 Jul 2009Member Level: Gold   Points : 2
T-SQL: The difference between @@identity, scope_identity(), and ident_current()
First of all, when discussing the three methods, you need to understand two concepts: session and scope. Session means the current connection that's executing the command. Scope means the immediate context of a command. Every stored procedure call executes in its own scope, and nested calls execute in a nested scope within the calling procedure's scope. Likewise, a SQL command executed from an application or SSMS executes in its own scope, and if that command fires any triggers, each trigger executes within its own nested scope.
Now that these two concepts are defined, here are the differences between the three identity retrieval methods:
• @@identity returns the last identity value generated in this session but any scope
• scope_identity() returns the last identity value generated in this session and this scope
• ident_current() returns the last identity value generated for a particular table in any session and any scope
To illustrate, imagine the following scenario: You execute an insert command on a Customer table, which fires an insert trigger that inserts a default Order for that customer in the Order table. Immediately after, before you execute another command, another session does the same thing.
• @@identity will return the identity value of the Order table, because the trigger on the Customer table executes after the insert into the Customer table, so the Order identity is the last identity generated in your session but any scope.
• scope_identity() will return the identify value of the row you inserted into the Customer table, because that's the last identity value generated at this level of scope in your session
• ident_current( Customer ) and ident_current( Order ) will return the identity of the rows inserted by the other session.
For most scenarios most of the time, what you will want is the value returned by scope_identity().




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: frequently used sql commands
Previous Resource: Stored Procedures Optimization Tips
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use