Habib
Habib

Reputation: 41

Auto Incremented Value Selection

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?

Upvotes: 1

Views: 63

Answers (2)

Habib
Habib

Reputation: 41

its done with this command.

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

Thank you.

Upvotes: 0

Livius
Livius

Reputation: 956

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

Upvotes: 1

Related Questions