SQL Server 返回“Arithmetic overflow error converting expression to data type int”

问题描述

对于大于INT最大值 (2,147,483,647) 的值,您需要使用COUNT_BIG (*)。

SELECT COUNT_BIG(*) AS [Records], SUM(t.Amount) AS [Total]
FROM   dbo.t1 AS t
WHERE  t.Id > 0
       AND t.Id < 101;

如果它发生在 中SUM,您需要转换AmountBIGINT.

SELECT COUNT(*) AS [Records], SUM(CONVERT(BIGINT, t.Amount)) AS [Total]
FROM   dbo.t1 AS t
WHERE  t.Id > 0
       AND t.Id < 101;

解决方法

当我运行这个命令时SUM()

SELECT COUNT(*) AS [Records],SUM(t.Amount) AS [Total]
FROM   dbo.t1 AS t
WHERE  t.Id > 0
       AND t.Id < 101;

我越来越,

Arithmetic overflow error converting expression to data type int.

知道它的原因是什么吗?