2

For my unit tests I have an automated process which builds a copy of a DB, table by table. I first of all run a SELECT INTO and add the primary key and any indices.

Some of the tables I copy with data, and for those without data I wish to reset the identity seed.

To achieve this I have the following T-SQL statement:

EXEC('TRUNCATE TABLE [' + @TableName + '];')
IF EXISTS (SELECT * FROM sys.identity_columns WHERE object_id = OBJECT_ID(@TableName) AND last_value IS NOT NULL)
BEGIN
    EXEC('DBCC CHECKIDENT (' + @TableName + ', reseed, 1)')
END

The query runs without any reported problems but when I inspect the table in design view the Identity Seed value is never reset.

Can anyone please tell me why the Identity Seed is not being reset, or what else I need to do?

UPDATE: As per some comments I have modified the sys.identity_columns to the following:

IF EXISTS (SELECT * FROM sys.identity_columns WHERE object_id = OBJECT_ID(@TableName))

When run, this returns the following message:

Checking identity information: current identity value '1'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Which looks promising. However, if I then refresh the table (in the Object Explorer) and look at it in the design view, the Identity Seed is unchanged, i.e. not 1 (or 0) but 135.

4

1 回答 1

2

在分配身份密钥时遇到了类似的问题(在我的情况下分配给更大的值)。在我的情况下,我发现如果我在运行 DBCHECKIDENT RESEED 之后运行 DBCC CHECKIDENT (TableName),它会完全“刷新”自身为新值。否则它将卡在较旧的值上。我不知道为什么,但它适用于我的情况(并且可能对其他人有用。)

于 2019-08-12T22:56:39.257 回答