@ Identity, scope_identity and ident_current

Source: Internet
Author: User

The content is referenced from:
@ Identity, scope_identity and ident_current

Http://www.cnblogs.com/xlong1900/archive/2008/09/01/1281001.html

@ Identity, scope_identity and ident_current

Functions of @ identity, scope_identity and ident_current in sqlserver
Similarities:
Both return values inserted into the identity column..

Differences:

A.@ Identity is not restricted by the scope (stored procedures, triggers, functions, or batch processing. Returns the last inserted identity value.. If the table inserted in different scopes in the program is different, the identity value inserted in the last table is returned. For example, scope A inserts the identity column on table T1, while triggers on table T1 Insert the identity column on table T2. @ Identity is used to return the inserted identity value on T2.

B.Scope_identity () is restricted by scope. Only the last identity value in the current scope is returned.. In the above example, if scope_identity () is used in the stored procedure, return the identity value of T1.

C.Ident_current () is not restricted by the scope. When calling it, you must provide the unique parameter indicating the table name. You can get the last identity value of any table you want, even if there is no insert action in your code. For example: ident_current ( ' T1 ' );


Experience:
Select different functions based on different application scenarios. If a new piece of data is added to the table during the write stored procedure and this identity value needs to be returned, scope_identity () is used because it is associated with the current session, it does not return a value you do not need. If you only want to get the last value of an identity column, ident_current () is the most convenient.

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.