As we prepare to move a database to a new environment, I needed a way to see what shape the database is in.
One of the things I wanted to check are the tables that have Identity Columns.
I basically wanted to see their uptake.
Here is a code that gets the last used Identity value for each table that has an identity Column.
One always wishes Life was awash with easy pickings. But, even a code like this betrays a few simple assumptions.
Let us see if we can identify a few:
- If we issue truncate table requests, the system automatically resets this value
- Not so when we issue deletes. Even when all records were removed due to an unfiltered deletes, the code branch for deletes does not go through the problem of checking that the table is now empty and have the the identity value re-calibrated
Here is a code from SQLServerPlanet.com ( http://sqlserverplanet.com/dba/using-dbcc-checkident-to-reseed-a-table-after-delete ) that gets the current max value from a table and resets the identity value:
The code above merely tracks the last Assigned identity value and not quite the value that will be assigned when a new entry is created.
The SQL Server instance possibly considers other things when assigning a new value, but is easier to determine and expose last used values.