Original: About Scope_identity () and @ @IDENTITY
Reference: Https://msdn.microsoft.com/zh-cn/library/ms190315.aspx
Scope_identity () involves scopes,
Scope Definition:
a scope is a module: a stored procedure, trigger, function, or batch. Therefore, if two statements are in the same stored procedure, function, or batch, they are in the same scope.
@ @IDENTITY Returns the last IDENTITY added within a session
Opening a new Query (tab) In Enterprise Manager indicates that a new session has been established and that all SQL statements entered in this tab are a session
, the result is the same, regardless of when you choose the following 2 sentences.
Each execution of a query or ctx.savechanges () in EF resets the exec sp_reset_connection, causing each execution to exist as a different session.
The return value of the above program is NULL, because Executestorequery is another session.
If statements and transactions fail, they change the current identity of the table, causing the values in the identity column to become incoherent. even if you do not commit a transaction that attempts to insert a value into the table, you can never roll back the identity value. For example, if an INSERT statement fails due to a ignore_dup_key conflict, the current identity value of the table will still increase.
About Scope_identity () and @ @IDENTITY