Applies to: SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance
Returns the last identity value inserted into an identity column in 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.
To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions because they return values that are inserted into identity columns.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT (Transact-SQL).
SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.
For example, there are two tables, T1 and T2, and an INSERT trigger is defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 by the trigger.
Assuming that both T1 and T2 have identity columns, @@IDENTITY and SCOPE_IDENTITY return different values at the end of an INSERT statement on T1. @@IDENTITY returns the last identity column value inserted across any scope in the current session. This is the value inserted in T2. SCOPE_IDENTITY() returns the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function returns the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.
Failed statements and transactions can change the current identity for a table and create gaps in the identity column values. The identity value is never rolled back even though the transaction that tried to insert the value into the table is not committed. For example, if an INSERT statement fails because of an IGNORE_DUP_KEY violation, the current identity value for the table is still incremented.
A. Using @@IDENTITY and SCOPE_IDENTITY with triggers
The following example creates two tables,
TY, and an INSERT trigger on
TZ. When a row is inserted to table
TZ, the trigger (
Ztrig) fires and inserts a row in
USE tempdb; GO CREATE TABLE TZ ( Z_id INT IDENTITY(1,1)PRIMARY KEY, Z_name VARCHAR(20) NOT NULL); INSERT TZ VALUES ('Lisa'),('Mike'),('Carla'); SELECT * FROM TZ;
Result set: This is how table TZ looks.
Z_id Z_name ------------- 1 Lisa 2 Mike 3 Carla
CREATE TABLE TY ( Y_id INT IDENTITY(100,5)PRIMARY KEY, Y_name VARCHAR(20) NULL); INSERT TY (Y_name) VALUES ('boathouse'), ('rocks'), ('elevator'); SELECT * FROM TY;
Result set: This is how TY looks:
Y_id Y_name --------------- 100 boathouse 105 rocks 110 elevator
Create the trigger that inserts a row in table TY when a row is inserted in table TZ.
CREATE TRIGGER Ztrig ON TZ FOR INSERT AS BEGIN INSERT TY VALUES ('') END;
FIRE the trigger and determine what identity values you obtain with the @@IDENTITY and SCOPE_IDENTITY functions.
INSERT TZ VALUES ('Rosalie'); SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]; GO SELECT @@IDENTITY AS [@@IDENTITY]; GO
Here is the result set.
/*SCOPE_IDENTITY returns the last identity value in the same scope. This was the insert on table TZ.*/` SCOPE_IDENTITY 4 /*@@IDENTITY returns the last identity value inserted to TY by the trigger. This fired because of an earlier insert on TZ.*/ @@IDENTITY 115
B. Using @@IDENTITY and SCOPE_IDENTITY() with replication
The following examples show how to use
SCOPE_IDENTITY() for inserts in a database that is published for merge replication. Both tables in the examples are in the
AdventureWorks2019 sample database:
Person.ContactType is not published, and
Sales.Customer is published. Merge replication adds triggers to tables that are published. Therefore,
@@IDENTITY can return the value from the insert into a replication system table instead of the insert into a user table.
Person.ContactType table has a maximum identity value of 20. If you insert a row into the table,
SCOPE_IDENTITY() return the same value.
USE AdventureWorks2012; GO INSERT INTO Person.ContactType ([Name]) VALUES ('Assistant to the Manager'); GO SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]; GO SELECT @@IDENTITY AS [@@IDENTITY]; GO
Here is the result set.
SCOPE_IDENTITY 21 @@IDENTITY 21
Sales.Customer table has a maximum identity value of 29483. If you insert a row into the table,
SCOPE_IDENTITY() return different values.
SCOPE_IDENTITY() returns the value from the insert into the user table, whereas
@@IDENTITY returns the value from the insert into the replication system table. Use
SCOPE_IDENTITY() for applications that require access to the inserted identity value.
INSERT INTO Sales.Customer ([TerritoryID],[PersonID]) VALUES (8,NULL); GO SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]; GO SELECT @@IDENTITY AS [@@IDENTITY]; GO
Here is the result set.
SCOPE_IDENTITY 29484 @@IDENTITY 89