Stackoverflow: Best way to get identity of inserted row

select * from order_item where item_id=(select @@IDENTITY)

https://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row:

@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes. You need to be careful here, since it’s across scopes. You could get a value from a trigger, instead of your current statement.

SCOPE_IDENTITY() returns the last identity value generated for any table in the current session and the current scope. Generally what you want to use.

IDENT_CURRENT(‘tableName’) returns the last identity value generated for a specific table in any session and any scope. This lets you specify which table you want the value from, in case the two above aren’t quite what you need (very rare). Also, as @Guy Starbuck mentioned, “You could use this if you want to get the current IDENTITY value for a table that you have not inserted a record into.”

The OUTPUT clause of the INSERT statement will…

你可能感兴趣的:(数据库)