About Scope_identity () and @ @IDENTITY

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.