为什么在 SQL Server 2008 R2 中设置当前标识值对我不起作用?

Why setting current identity value is not working for me in SQL Server 2008 R2?

我正在使用 SQL Server 2008 R2。

我有一个 table seq_audit,它有一个标识列。这是table:

的定义
CREATE TABLE [dbo].[seq_audit]
(
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [value] [bit] NULL,

    PRIMARY KEY CLUSTERED ([id] ASC)
          WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
                IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, 
                ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

table 是空的,之前没有任何行。

要检查其当前标识值,我 运行 这个命令:

DBCC CHECKIDENT (seq_audit, NORESEED) 
GO

这是我得到的结果:

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

我想将其当前标识值设置为 15953711。所以我 运行 这个命令:

DBCC CHECKIDENT (seq_audit, RESEED, 15953711)
GO

这是我得到的结果:

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

我认为它有效,所以我再次通过 运行 这个命令检查它的当前身份:

DBCC CHECKIDENT (seq_audit, NORESEED) 
GO

但没想到我得到的结果是:

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

所以不知何故当前标识值的设置不起作用。为什么?我在这里做错了什么?

这是因为您的 table 是空的。尝试添加一条记录,然后一切正常。我已经试过了,可以确认它有效。

此外,如果您使用 SQL 服务器管理工​​作室,您可以使用设计功能来更改种子值。并手动添加和删除记录。

https://msdn.microsoft.com/es-es/library/ms176057(v=sql.120).aspx

DBCC CHECKIDENT ( table_name, RESEED, new_reseed_value )

Current identity value is set to the new_reseed_value. If no rows have been inserted into the table since the table was created, or if all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses new_reseed_value + the current increment value.

还有为什么你不在创建时启动种子 table IDENTITY?

Sql Fiddle Demo

CREATE TABLE [dbo].[seq_audit](
    [id] [bigint] IDENTITY(15953711,1) NOT NULL,
    [value] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY];

您要插入的第一个值的标识值为 15953711。 您可以开始插入了。