Obtain the latest ID by SQL, scope_identity (), @ identity, ident_current

Source: Internet
Author: User

-- Scope_identity () the return value of this method is null, because it returns the last value of the identity in the current session and scope.

-- @ Identity: returns the value of identity in any scope in the current session.

-- Ident_current ('table name') returns the value of the last identity of the specified table, regardless of the session and scope.

@ Identity
Returns the last inserted id value.
 
Syntax
@ Identity
 
Return type
Numeric
 
Note
After an insert, select into, or large-capacity copy statement is completed, @ identity contains the final id value generated by this statement. If this statement does not affect any table with an ID column, @ identity returns NULL. If multiple rows are inserted, multiple ID values are generated. @ identity returns the final id value. If one or more triggers are triggered to execute the insert operation that generates the id value, @ identity is called immediately after the statement is executed, and the last id value generated by the trigger is returned. If the insert or select into statement fails, the large-capacity copy fails, or the transaction is rolled back
The @ identity value is not restored to the previous setting.
 
The functions of @ identity, scope_identity, and ident_current are similar in returning the last value of the @ identity column inserted to the table.
 
@ Identity and scope_identity return the last id value generated in all tables of the current session. However, scope_identity only returns values within the current scope, and @ identity is not limited to specific scopes.
 
Ident_current is not restricted by the scope and session, but by the specified table. Ident_current returns the id value generated for a specific table in any session and in any scope.

-------- A separation line -------------

Differences between the three methods that return the maximum value:
 
Ident_current returns the last generated id value for any session and specific tables in any scope.
 
 
@ Identity returns the last generated id value for any table in all scopes of the current session.
 
 
Scope_identity returns the last generated id value for the current session and any table in the current scope. (Prevent returning the identity value of insert in the trigger)

-------- Another one -------------

@ Identity
Returns the last inserted id value.
 
Syntax
@ Identity
 
Return type
Numeric

-------- Another one -------------

Note
After an insert, select into, or large-capacity copy statement is completed, @ identity contains the final id value generated by this statement. If this statement does not affect any table with an ID column, @ identity returns NULL. If multiple rows are inserted, multiple ID values are generated. @ identity returns the final id value. If one or more triggers are triggered to execute the insert operation that generates the id value, @ identity is called immediately after the statement is executed, and the last id value generated by the trigger is returned. If the insert or select into statement fails, the large-capacity copy fails, or the transaction is rolled back
The @ identity value is not restored to the previous setting.
 
The functions of @ identity, scope_identity, and ident_current are similar in returning the last value of the @ identity column inserted to the table.
 
@ Identity and scope_identity return the last id value generated in all tables of the current session. However, scope_identity only returns values within the current scope, and @ identity is not limited to specific scopes.
 
Ident_current is not restricted by the scope and session, but by the specified table. Ident_current returns the id value generated for a specific table in any session and in any scope. For more information, see 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.