当我运行这个命令时SUM()
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.
知道它的原因是什么吗?
对于大于INT最大值 (2,147,483,647) 的值,您需要使用COUNT_BIG (*)。
INT
SELECT COUNT_BIG(*) AS [Records], SUM(t.Amount) AS [Total] FROM dbo.t1 AS t WHERE t.Id > 0 AND t.Id < 101;
如果它发生在 中SUM,您需要转换Amount为BIGINT.
SUM
Amount
BIGINT
SELECT COUNT(*) AS [Records], SUM(CONVERT(BIGINT, t.Amount)) AS [Total] FROM dbo.t1 AS t WHERE t.Id > 0 AND t.Id < 101;