SQL Server SELECT @ @IDENTITY pits encountered

Source: Internet
Author: User

Always get the current inserted ID at the time of writing the stored procedure with @ @IDENTITY

But today when using @ @IDENTITY is involved in the current data insertion will have an INSERT trigger occurs, found with the actual inserted ID value is not on, online query, with scope_identity () just fine

Select scope_identity ()

Returns the value of the identity column of the last row of the data table above operation;

Returns the last identity value in the identity column inserted into the same scope. A scope is a module-a stored procedure, trigger, function, or batch. Therefore, if two statements are in the same stored procedure, function, or batch, they are in the same scope.

SELECT   @ @IDENTITY

Returns the value of the identity column of the last row of the last data table above operation;
To create a table:

Create Table int Identity (1,1notnullvarchar(notnull)

Insert data:

Insert  into Values (' who am I 'Selectscope_identity()

Stored procedures:

CREATE PROCEDURE [dbo]. [sp_user] (@F_NameintasbeginTran  insertinto_t_user  Insertinto  values(@F_Name)Selectscope_ IDENTITY()

SQL Server SELECT @ @IDENTITY pits encountered

Related Article

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.