SQL Server TSQL Get Last Identity Seed After Insert

Tags: TSQL, SQL Server, identity

Use SCOPE_IDENTITY() if you are inserting a single row and want to retrieve the ID that was generated.

CREATE TABLE #a(identity_column INT IDENTITY(1,1), x CHAR(1));

INSERT #a(x) VALUES('a');

SELECT SCOPE_IDENTITY();

Result:

----
1

Use the OUTPUT clause if you are inserting multiple rows and need to retrieve the set of IDs that were generated.

INSERT #a(x) 
  OUTPUT inserted.identity_column 
  VALUES('b'),('c');

Result:

----
2
3

source:

https://dba.stackexchange.com/questions/124847/best-way-to-get-last-identity-inserted-in-a-table

No Comments

You must log on to comment.