Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SCOPE_IDENTITY or @@IDENTITY

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-08-23 : 17:32:39
I am creating a proc that will insert data into a few tables but after the first insert I need to get the uniqie identifier that is placed into the identy column of the newly made row. I know SCOPE_IDENTITY and @@IDENTITY can return that but I am not sure which is best to use. I would think it best to use SCOPE_IDENTITY to get the ID that was created in the one instance of the proc but I might be configuing it with wanting to get it from that one session of the proc. Can anyone tell me which would be better to use to avoide getting the wrong ID in the off case that two people insert new data at the same time using the proc?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-23 : 18:00:55
SCOPE_IDENTITY() is what you want.

Getting the wrong identity has nothing to do with multiple users executing the same sproc.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-24 : 01:37:24
see this to understand difference

http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2011-08-24 : 07:14:27
You may want to look at using the OUTPUT clause of INSERT as detailed in the Microsoft reply to the following:

http://connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=328811
Go to Top of Page
   

- Advertisement -