SQL:类型INT的溢出错误

时间:2017-01-26 18:30:13

标签: tsql

我有以下SQL代码,我正在努力创建二阶多项式回归的公式。当我这样做时,@ SUMX4收到如下错误:

Msg 232,Level 16,State 3,Line 18 int类型的算术溢出错误,值= 2342560000.000000。

我将所有变量转换为BIGINT并仍然出现此错误。有什么想法吗?

create table Set1
(X INT,
Y INT)
INSERT INTO Set1 VALUES
 (220, 630)
,(350, 940)
,(450, 1140);


DECLARE @SUMX BIGINT
DECLARE @SUMY BIGINT
DECLARE @SUMX2 BIGINT
DECLARE @SUMY2 BIGINT
DECLARE @SUMX3 BIGINT
DECLARE @SUMX4 BIGINT
DECLARE @SUMXX BIGINT
DECLARE @SUMXY BIGINT
DECLARE @SUMXX2 BIGINT
DECLARE @SUMX2Y BIGINT
DECLARE @SUMX2X2 BIGINT

SET @SUMX = (SELECT SUM(X) FROM SET1)
SET @SUMY = (SELECT SUM(Y) FROM SET1)
SET @SUMX2 = (SELECT SUM(POWER(X,2)) FROM SET1)
SET @SUMY2 = (SELECT SUM(POWER(Y,2)) FROM SET1)
SET @SUMX3 = (SELECT SUM(POWER(X,3)) FROM SET1)
SET @SUMX4 = (SELECT SUM(POWER(X,4)) FROM SET1)


PRINT CONCAT('SUMX: ', @SUMX)
PRINT CONCAT('SUMY: ', @SUMY)
PRINT CONCAT('SUMX2: ', @SUMX2)
PRINT CONCAT('SUMY2: ', @SUMY2)
PRINT CONCAT('SUMX3: ', @SUMX3)
PRINT CONCAT('SUMX4: ', @SUMX4)
PRINT CONCAT('SUMXX: ', @SUMXX)
PRINT CONCAT('SUMXY: ', @SUMXY)
PRINT CONCAT('SUMXX2: ', @SUMXX2)
PRINT CONCAT('SUMX2Y: ', @SUMX2Y)
PRINT CONCAT('SUMX2X2: ', @SUMX2X2)

1 个答案:

答案 0 :(得分:1)

SUM返回与您求和的表达式相同的数据类型。尝试像这样投射到bigint:

SET @SUMX = (SELECT SUM(CAST(X AS BIGINT)) FROM SET1)
...
SET @SUMX2 = (SELECT SUM(POWER(CAST(X AS BIGINT),2)) FROM SET1)
...

所有金额