The use of “Auto Increment Identitiy Column” in tables is common. In some cases, we may want to find the last Identitiy value generated. In this article, we will use the IDENT_CURRENT function to find the last generated identitiy value.
Example:
First, we create a table with the help of the following script and add a few records into this table.
1 2 3 4 5 6 | CREATE TABLE [dbo].[MyTable] ([ID] INT IDENTITY(1,1), [Name] VARCHAR(20) NULL ) INSERT [dbo].[MyTable] VALUES ('Hakan GURBASLAR'),('Nurullah CAKIR'),('Faruk ERDEM') |
Find Last Generated identity value with IDENT_CURRENT function:
1 | SELECT IDENT_CURRENT('[dbo].[MyTable]') AS LastGeneratedIdentityValue |