SQL Server $$Identity

I've noticed at times that using

set @VariablenID = SCOPE_IDENTITY()  will work when

set @PaymentTransactionID = @@Identity can give me problems and bomb out in my .Net web pages on the output.

Is one better than the other and what's the difference?
Who is Participating?
Aneesh RetnakaranConnect With a Mentor Database AdministratorCommented:
SCOPE_IDENTITY() is better as it gives you identity value for the current session (scope) while the other returns the identity value which may or may not be in the current scope
lrbristerAuthor Commented:

I'm going to accept the answer above...seen your replies for years on EE and trust you...

Now...big question...
You have a link that explains "Scopes" so I can ubderstand what you just said?  ;)
Aneesh RetnakaranConnect With a Mentor Database AdministratorCommented:
FROM Books online

SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions because they return values that are 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. For more information, see IDENT_CURRENT (Transact-SQL).

SCOPE_IDENTITY and @@IDENTITY return the last identity values that are 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.

For example, there are two tables, T1 and T2, and an INSERT trigger is defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 by the trigger.

Assuming that both T1 and T2 have identity columns, @@IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on T1. @@IDENTITY will return the last identity column value inserted across any scope in the current session. This is the value inserted in T2. SCOPE_IDENTITY() will return the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function will return the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.

Failed statements and transactions can change the current identity for a table and create gaps in the identity column values. The identity value is never rolled back even though the transaction that tried to insert the value into the table is not committed. For example, if an INSERT statement fails because of an IGNORE_DUP_KEY violation, the current identity value for the table is still incremented.

there are examples for this on books online
lrbristerAuthor Commented:
Perfeect...as always
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.