消息232,级别16,状态3,行5类型int的算术溢出错误

时间:2016-08-10 06:29:32

标签: sql sql-server sql-server-2008 tsql msbi

我怎样才能找到两个变量之间的区别?

error is Msg 232, Level 16, State 3, Line 5
Arithmetic overflow error for type int, value = -39827814763.955299.

Msg 232, Level 16, State 3, Line 12
Arithmetic overflow error for type int, value = -39827814763.950142.

查询在下面.. TIA

declare  @sql int 
declare @sql1 int
declare @sql3 int

set @sql = (select sum(sfg.amount) as VM_Amount_LCY
  from Setup_Company sc inner join stg_dim_dimension02 sdd on 
sdd.company_id = sc.company_id inner  join stg_fact_dimension_set_entry fdse on fdse.dim02_sk = sdd.dimension_id and fdse.company_id=sdd.company_id
inner join stg_fact_gl sfg on sfg.[dimension set id] = fdse.[Dimension Set ID] and sfg.company_id= fdse.company_id )



set @sql1 = (select sum(sfg.amount) as BI_Amount_LCY from [NORRIQ Drink-IT BI 2013 India].dbo.Setup_Company sc inner join [NORRIQ Drink-IT BI 2013 India].dbo.dim_dimension02 sdd on 
sdd.company_id = sc.company_id inner  join [NORRIQ Drink-IT BI 2013 India].dbo.fact_dimension_set_entry fdse on fdse.dim02_sk = sdd.dimension_id and fdse.company_id=sdd.company_id
inner join [NORRIQ Drink-IT BI 2013 India].dbo.fact_gl sfg on sfg.[dimension set id] = fdse.[Dimension Set ID] 
and sfg.company_id= fdse.company_id )


select @sql3 = (-(@sql) - (-(@sql1)))

2 个答案:

答案 0 :(得分:1)

-39827814763.955299

以上值为十进制,您试图将其存储为INT.INT可以存储以下范围内的数字..

-2,147,483,648 to 2,147,483,647

您可以使用BIGINT来克服Ari主题溢出..

declare  @sql BIGINT 
declare @sql1 BIGINT 
declare @sql3 BIGINT 

但是如果您需要精度,则使用上述数据类型会丢失精度,请使用如下所示的数字。

 declare  @sql numeric(22,6)

答案 1 :(得分:0)

SUM中的表达式类型决定了案例sfg.amount中的返回类型。在执行SUM之前,您可以将CAST作为例如BIGINT,如下所示:

SELECT SUM( CAST( sfg.amount AS BIGINT )) ....