0

@ @ IDENTITY returned the last row inserted for a user or for all users?

 3 years ago
source link: https://www.codesd.com/item/identity-returned-the-last-row-inserted-for-a-user-or-for-all-users.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

@ @ IDENTITY returned the last row inserted for a user or for all users?

advertisements

@@IDENTITY return last inserted identity values for every table, so for all scopes.

I have app that allow users insert data in database for all day. On the other side, I have some scripts for some data imports where I use @@IDENTITY in cursor to fetch every row identity and place it in some log table.

Now, will it be possible to @@IDENTITY to pick identity from users inserts, and to make mess in my log table?


Use SCOPE_IDENTITY()

Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

(the instances where you need to use anything but SCOPE_IDENTITY() are few).

As @Damien_The_Unbeliever pointed out, the above link includes a discussion of the uses/benefits/drawbacks to using @@IDENTITY, SCOPE_IDENTITY() and IDENT_CURRENT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK