rwitt 95744 (5/7/2014)
I think placing this in my test data script will resolve the issue (or rather, cover it, for both situations):DECLARE @DELETEDFROM_<tablename> bit = 1
IF (SELECT IDENT_CURRENT('<tablename>') = 1 AND (SELECT COUNT(*) FROM <tablename>) = 0
SET @DELETEDFROM_<tablename> = 0-- New table, has never contained any rows
-- Need to run the above prior to clearing the table
-- Run this after clearing the table
IF @DELETEDFROM_<tablename> = 0
BEGIN
-- New table, has never contained rows; CHECKIDENT(RESEED) treats it as a TRUNCATEd table
DBCC CHECKIDENT ('<tablename>', reseed, 1)
END
ELSE
BEGIN
-- Table exists & was DELETEd FROM; need to use different RESEED value
DBCC CHECKIDENT ('<tablename>', reseed, 0) -- Needed b/c DELETEd rather than TRUNCATEd this table
END
For efficiency, rather than counting all the rows, simply check to see if one exists:
IF (SELECT IDENT_CURRENT('<tablename>') = 1 AND NOT EXISTS(SELECT TOP (1) * FROM <tablename>)
SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.