yifanwu
yifanwu

Reputation: 1645

synchronization issue with SCOPE_IDENTITY()

sorry if this question is dumb but I cant seem to find the answer. Just starting out with SQL. I was reading the answers for Get the last inserted row ID (with SQL statement), and it seems that SCOPE_IDENTITY is "the latest inserted value". Is this operation locked right after insert because if not, then another newly inserted row may become something that got inserted right after the insertion I intended to get the ID of.

Thanks for the help!

Upvotes: 2

Views: 300

Answers (2)

Rikalous
Rikalous

Reputation: 4574

There is a further alternative to using Scope_Identity that you should also study - have a read about using the OUTPUT clause: http://msdn.microsoft.com/en-us/library/ms177564(v=sql.100).aspx/html

Upvotes: 1

Jagz S
Jagz S

Reputation: 907

To get latest inserted row information through SQL query you have three options:

@@IDENTITY: it returns the last identity value generated for any table in the current session, across all scopes

SCOPE_IDENTITY: it returns the last identity value generated for any table in the current session and the current scope.

IDENT_CURRENT: it returns the last identity value generated for a specific table in any session and any scope

Upvotes: 5

Related Questions