根据Sql Server,任何计算列只有在确定性时才能被索引。 ie。,认为列a,b和c是INT数据类型,c = a + b 。现在列C可以被索引,但是当列a或b保存最大的整数值时它将引发算术错误,是否有任何解决方案?
感谢。
答案 0 :(得分:2)
create table TA (
ID int not null primary key,
a int not null,
b int not null,
c as a+b
)
go
create index IX_TA_c on TA (c)
go
insert into TA(ID,a,b) values (1,1,2147483647)
插入获取:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
如果您希望计算始终有效,请更改计算,以便至少有一列强制为bigint
(因此数学和c
的结果类型都是bigint
也是):
c as CAST(a as bigint)+b