【发布时间】:2011-01-24 10:43:42
【问题描述】:
假设我们有一张桌子:
create table MYTABLE (
id int IDENTITY(1,1)
,name varchar(10)
)
我们必须在表中插入很多行。
有人知道当生成的标识值超过最大整数值 (2^63-1) 时会发生什么吗?
【问题讨论】:
标签: sql-server sql-server-2008 identity identity-column
假设我们有一张桌子:
create table MYTABLE (
id int IDENTITY(1,1)
,name varchar(10)
)
我们必须在表中插入很多行。
有人知道当生成的标识值超过最大整数值 (2^63-1) 时会发生什么吗?
【问题讨论】:
标签: sql-server sql-server-2008 identity identity-column
一个例子
create table dbo.MYTABLE (
id tinyint IDENTITY(254,1)
,name varchar(10)
)
GO
INSERT dbo.MYTABLE (name) VALUES ('row 254')
GO
INSERT dbo.MYTABLE (name) VALUES ('row 255')
GO
INSERT dbo.MYTABLE (name) VALUES ('broke')
GO
给予
Msg 8115, Level 16, State 1, Line 1
Arithmetic overflow error converting IDENTITY to data type tinyint.
Arithmetic overflow occurred.
【讨论】:
会发生错误,插入会丢失。
消息 8115,第 16 级,状态 1,第 2 行 将 IDENTITY 转换为数据类型 int 的算术溢出错误。 发生算术溢出。
【讨论】:
您可以使用非常小的标识列轻松测试这一点,例如 decimal(1,0):
create table IdentityOverflow (id decimal(1,0) identity)
while 1=1
insert IdentityOverflow default values
就像 Oded 所说,这打印出来:
Arithmetic overflow error converting IDENTITY to data type decimal.
这甚至适用于最大的整数:
create table IdentityOverflow (
id decimal(38,0) identity(1,10000000000000000000000000000000000000))
while 1=1
insert IdentityOverflow default values
【讨论】: