views:

25

answers:

1

I was curious if there is a way to tell if an IDENTITY column has ever been incremented if there is no data within the table. (i.e. data item was inserted, then deleted)

+1  A: 

For SQL Server you can use this approach:

SELECT IDENT_SEED(TABLE_NAME) AS Seed,
    IDENT_INCR(TABLE_NAME) AS Increment,
    IDENT_CURRENT(TABLE_NAME) AS Current_Identity,
    TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1
    AND TABLE_TYPE = 'BASE TABLE'

Note that you can reset the seed value in SQL Server, so the above apporach only tells you the current state of the seed, not whether there was ever any data in the table or not.

RedFilter
The reason I am asking is because I was messing around with my test tables and truncated all of the tables even if they have never been used before. What I noticed was that the tables that have never been used before, when data is inserted the IDENTITY stays at ZERO (0) just for that one record and then it increments to ONE (1) after. Have you seen this behavior before? I am truncating because I have relationships associated with the tables
mattgcon
This is a common SQL Server issue. You can run `DBCC CHECKIDENT('MyTable', RESEED, 0)` after you truncate to make the identity starts at 1.
RedFilter
That is the thing I cannot truncate as msot of these tables with this is has a foreign relationship
mattgcon