【发布时间】:2014-01-06 05:10:08
【问题描述】:
我使用的是 SQL Server 2012,并且我有一个 bigint 类型的主键列。
有时在新插入时,新的主键需要 1000 或 10000 的巨大跳跃。
例如:
ID
--
1
2
3
4
5
6
7
8
9
10001
10002
为什么会这样?这是一个错误吗?
【问题讨论】:
标签: sql sql-server-2012
我使用的是 SQL Server 2012,并且我有一个 bigint 类型的主键列。
有时在新插入时,新的主键需要 1000 或 10000 的巨大跳跃。
例如:
ID
--
1
2
3
4
5
6
7
8
9
10001
10002
为什么会这样?这是一个错误吗?
【问题讨论】:
标签: sql sql-server-2012
这是 SQL Server 2012 的行为。
要解决此问题,您需要确保在序列创建/这样的属性中添加 NO CACHE 选项。
create sequence Sequence1
as int
start with 1
increment by 1
no cache
go
create table Table1
(
id int primary key,
col1 varchar(50)
)
go
create trigger Trigger1
on Table1
instead of insert
as
insert Table1
(ID, col1)
select next value for Sequence1
, col1
from inserted
go
insert Table1 (col1) values ('row1');
insert Table1 (col1) values ('row2');
insert Table1 (col1) values ('row3');
select *
from Table1
希望这会有所帮助..
【讨论】: