@@IDENTITY vs SCOPE_IDENTITY()

Published 03-20-2006 4:26 AM | jokiz
I have read a number of books/articles and I wonder why they always use the global variable @@IDENTITY in obtaining the generated identity after an INSERT operation.  They should be using SCOPE_IDENTITY() instead which is safer due to triggers.  With SCOPE_IDENTITY(), is there really a use for @@IDENTITY?
Filed under: ,

Comments

# bonskijr said on March 20, 2006 6:10 AM:

Apparently @@IDENTITY is a holdover from SQL Server 7 when there was no SCOPE_IDENTITY function yet. You're aware of the differences and you should always use SCOPE_IDENITY at all times, @@IDENTITY however has the advantage of being available  in TRIGGERS. SCOPE_IDENTITY will return NULL when used in a TRIGGER. Not to mention also that the author might have been targetting sql server 7 and up and wasn't aware of the new functions IDENT_CURRENT and SCOPE_IDENTITY

# jokiz said on March 20, 2006 5:14 PM:

duplicate post detected... :p

# bonskijr said on March 21, 2006 4:36 AM:

hehe sensya na.. :P