Transact-SQL returns the identity of the most recently inserted row

Source: Internet
Author: User
Tags sessions

We often encounter the following question: How do I get the identity of the latest insert record?

Such requirements are common, and the three methods of obtaining identities provided by Transact-SQL are described below.
(1) Ident_current returns the most recent identity value generated for a session and the specified table in the domain.
(2) @ @IDENTITY Returns the most recent identity value generated for a table in the current session across all scopes.
(3) Scope_identity returns the most recent identity value generated for the current session and a table in the current scope.

(1) Ident_current returns the most recent identity value generated for a session and the specified table in the domain.
Syntax: ident_current (' table_name ')
Description: The most recent identity obtained through Ident_current is the session and scope, which means that no matter who inserts a new row into the specified table, it only gets the identity of the most recent row for that table.
Example: Open two query Windows (two sessions), write INSERT statements in each of the two query Windows, and insert them into the same table.
Write Select Ident_current (' table_name ') in any of the query windows;
The result is that the return value of select is the identity that was generated when the table was last inserted.
Note: If you want to get the line ID you just inserted, it is recommended not to use this method;
This method is recommended if you want to obtain the most recent row identifier for the entire table;

(2) @ @IDENTITY Returns the most recent identity value generated for a table in the current session across all scopes.
    Syntax: @ @IDENTITY
    Description: The first @ @IDENTITY gets the current session, that is, does not get the new row identity that someone else inserted. The only difference between it and scope_identity is that the @ @IDENTITY is cross-scoped, and scope_identity is the current scope.
    Example: Two tables A and B, table A establishes a trigger, and once a new record is added, the trigger adds a new record to table B.
          adds a record to table A.
          Get the latest identity SELECT @ @IDENTITY;
           results found that the identity of the new row in table B was returned.
    Note: If you have only one table added to your current session, you can get the most recently inserted row ID with @ @IDENTITY.
          If there is a trigger he inserts, you cannot use this method.

(3) Scope_identity returns the most recent identity value generated for the current session and a table in the current scope.
    Syntax: scope_identity ()
    Description: This method gets the latest line identity and is similar to the @ @IDENTITY. Only the scope is the current scope.
    Example: Method with (2);
          results found the return value is a new row identifier for table A.
    Example: Open two query Windows (two sessions), write INSERT statements in each of the two query Windows, and insert them into the same table.
          Write Select scope_identity () in any of the query windows;
           results found that the return value of select is the identity of the row that the window is inserted into.
    Note: This method is most reliable if you want to get the latest inserted row ID.

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.