You may be familiar with @ identity in SQL Server. It is known that it is used to obtain the identity value of the last inserted data in the data table.
For example, if Table A has a field with ID auto-increment of 1 and the id value is 100, if I insert a piece of data to table A and
Select @ identity, then it returns the value of 101, the last identity domain (that is, the ID domain.
Now the question is, why should we use @ identity with caution? The reason is that @ identity always obtains the value of the auto-increment field of the last change data,
The scope constraints of the change operation are ignored. For example, I have two tables, table A and Table B. Now I have defined an insert trigger on Table,
When a piece of data is inserted in Table A, a piece of data is automatically inserted in table B. Note that there are two atomic operations: Insert a piece of data in a, and then insert a piece of data in B.
Now, assume that both table A and table B have an identity auto-incrementing domain. After inserting a data entry in Table A, we use
In the select @ identity output, is the output A or B's auto-incrementing Domain value? The answer is obvious, who will output the last insert,
That's B. So I wanted to get the auto-incrementing Domain value of A, and the result obtained the auto-incrementing Domain value of B. A Bug was born along with it.
Will affect the entire system data chaos.
Therefore, we recommend that you use @ identity with caution, and use the scope_identity () function whenever possible. Scope_identity ()
The value of the last auto-increment domain is obtained, but it is only within the range of an operation. Unlike @ identity, it is the last operation to take the global operation.
The value of the auto-increment field generated.
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.