Select SCOPE_IDENTITY () and SELECT @ IDENTITY usage

Source: Internet
Author: User

Select SCOPE_IDENTITY ()

Returns the value of the IDENTITY column in the last row of the data table in the preceding operation;

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

SELECT @ IDENTITY

Returns the value of the IDENTITY column in the last row of the last data table in the preceding operation;
Create a table:

Create table T_User (F_ID int identity (1, 1) not null, F_Name varchar (20) not null)

Insert data:

Insert into T_User (F_Name) values ('who I am ') Select SCOPE_IDENTITY ()

Stored Procedure:

Create procedure [dbo]. [sp_User] (@ F_Name int)
Begin tran Insertinto_T_User
Insert into dbo. T_User (F_Name) values (@ F_Name)
Select SCOPE_IDENTITY ()

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.