Identity of an Inserted Row

The identity of an inserted row is the last identity column value in a table which was automatically generated by the sql server database system when a new row is added to a table that has an identity column with auto increment set with initial and increment values like the below example.

EmployeeID Int NOT NULL IDENTITY (1,1)

In the above example, the EmployeeID column is created as an identity column to auto increment by 1 with an initial value starting from 1. When a new row is created in the table the EmployeeID will be 1 and it will increment the value by 1 after each row insert subsequently.

How to Get the Identity of an Inserted Row in SQL Server

In SQL Server, we can use an Identity column, which will automatically increment the ID value in a table. The Identity column is a unique, sequential, and non-null value for each row in a table. The identity value is auto-incremented using the IDENTITY command. There could be situations when this auto-incremented value of the Identity column may be required.

In this article, we will discuss the different options available to get the identity value of the inserted row.

Similar Reads

Identity of an Inserted Row

The identity of an inserted row is the last identity column value in a table which was automatically generated by the sql server database system when a new row is added to a table that has an identity column with auto increment set with initial and increment values like the below example....

Getting the Identity Value from the Inserted Row

When an identity column is incremented while a new row is inserted, the same value may be required in the SQL to update or insert in another column or for any other processing of data like in Store procedure to update the EmployeeID value from employees table into a Managers table like the example below:...

Conclusion

Getting the inserted value from an Identity column is useful in many situations and this can done using the system functions instead of going to the table and finding the last identity value. But there can incorrect identity values returned when there are multiple inserts in a table or across many tables. So use the right system function to get the identity value based on session and scope of the insert done. Also note that the identity value is returned only from a Identity Column set with auto increment option....

Contact Us