Identification of IDENTITY, SCOPE_IDENTITY and IDENT_CURRENT

Source: Internet
Author: User

Prerequisites: SQL Server IDENTITY keyword
The IDENTITY keyword represents a function, not the identity attribute. This function is not available in access, so this statement cannot be used in access. Syntax:Identity (data type, identification seed, identification growth)
Example: select identity (data type, identify seed, identify growth volume) AS column name into new table from original table name

Each table can haveAuto-increment ColumnIt is well known that the IDENTITY column. An IDENTITY column is always processed as the primary key of the table (therefore, an IDENTITY column cannot exist for multiple primary keys ). We can also see that it is easy to specify the Starting number and increasing value.

 

SQLServer @ IDENTITY,SCOPE_IDENTITYAnd IDENT_CURRENTFunction
Similarities:

Both return values inserted into the IDENTITY column.

Differences:

A. @ IDENTITY is not supportedFunctionDomain (stored procedure, trigger, function, or batch processing) restrictions. Returns the last inserted IDENTITY value. If differentFunctionIf the table inserted by the domain is different, the IDENTITY value inserted in the last table is returned. For example:FunctionDomain A (Stored Procedure) 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. Example:
The following example inserts a row into a table with an ID column and uses @ IDENTITY to display the id value used in the new row.

Program code Insert INTO jobs (job_desc, min_lvl, max_lvl)
VALUES ('accountant', 12,125)
Select @ identity as 'identified'

B.SCOPE_IDENTITY() SubjectFunctionDomain restrictions, only returns the currentFunctionThe last IDENTITY value in the domain. In the preceding exampleSCOPE_IDENTITY(), Then return the IDENTITY value of T1.

C. IDENT_CURRENT () is notFunctionWhen calling a table name, you must provide the unique parameter that represents the table name. You can obtain the last IDENTITY value of any table you want, even if there is no insert action in your code. Example: IDENT_CURRENT ('t1 ');

Experience:
Select different functions based on different application scenarios. If a new data entry is added to the table during the write stored procedure and the IDENTITY value needs to be returned, useSCOPE_IDENTITY() Because it is associated with the current session, no value you don't need is returned. If you only want to get the last value of an IDENTITY column, it is most convenient to use IDENT_CURRENT ().

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.