The problem found when the identity value of the Insert trigger table is taken _mssql

Source: Internet
Author: User
The problem is this:
There is an INSERT trigger on the T1 table that automatically inserts a record into the T2 table when the data is inserted.
So when I insert new data on the T1 table, when I take the @ @IDENTITY, the ID value returned is the value of the new record in the T2 table.

Quickly check the next MSDN, the original @ @IDENTITY there are so much attention:

After an INSERT, SELECT into, or bulk copy statement completes, the @ @IDENTITY contains the last identifying value of the statement generation. If the statement does not affect any table that contains an identity column, the @ @IDENTITY returns NULL. If more than one row is inserted and more than one identity value is generated, the @ @IDENTITY returns the last generated identity value. If the statement triggers one or more triggers and the trigger performs an insert operation that generates an identity value, calling the @ @IDENTITY immediately after the statement executes returns the last identity value generated by the trigger. If a trigger is triggered after an insert operation on a table that contains an identity column, and the trigger performs an insert operation on another table that does not have an identity column, the @ @IDENTITY returns the identity value that was first inserted. When an INSERT or SELECT into statement fails or a bulk copy fails, or a transaction is rolled back, the @ @IDENTITY value does not revert to the previous setting.


If statements and transactions fail, they change the current identity of the table, causing inconsistencies in the values in the identity column. The identity value can never be rolled back even if a transaction that attempted to insert a value into the table is not committed. For example, if an INSERT statement fails because of a ignore_dup_key conflict, the current identity value of the table will still increase.
@ @IDENTITY, scope_identity, and ident_current are similar functions because they all return the last value inserted into the IDENTITY column of the table.
@ @IDENTITY and scope_identity can return the last identity value generated in all tables in the current session. However, scope_identity only returns values within the current scope, while the @ @IDENTITY is not limited to a specific scope.
Ident_current are not limited by scope and session, but are limited by the specified table. Ident_current can return the identity values generated for a particular table in any session and in any scope.
The scope of the @ @IDENTITY function is the current session on the local server that executes the function. This function cannot be applied to a remote or linked server. To obtain an identity value on a different server, execute the stored procedure on the remote server or linked server and cause the stored procedure (executed in the context of the remote or linked server) to collect the identity value and return it to the connection that made the call on the local server.


So when you operate on multiple tables, the most useful
Select Scope_identity () and select Ident_current (' T1 ')

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.