1

I would like to select the max value in sql of a column ABC in table XYZ where that column ABC is on auto increment. Lets say that I have insert several rows into the table XYZ and then deleted those rows. We all know that auto increment value for that table would not be zero any more even if the table's rows are deleted.

Now in this case how would I get the max value or the highest value last used but deleted?

EzLo
  • 13,780
  • 10
  • 33
  • 38
Habib
  • 41
  • 1
  • 1
  • 10

2 Answers2

1

Use mssql feature IDENT_CURRENT( 'table_name' )

Example:

DECLARE @currentIdentity INT = IDENT_CURRENT('schemaName.TableName')

SELECT @currentIdentity

Helpful in your case can be https://stackoverflow.com/a/1280757/3114457

EzLo
  • 13,780
  • 10
  • 33
  • 38
Livius
  • 958
  • 1
  • 6
  • 19
0

its done with this command.

    Declare @abc varchar(30);
    Set @abc = ident_Current ('Companies');
    print @abc;

Thank you.

Habib
  • 41
  • 1
  • 1
  • 10