将temptable列(十进制)转换为十进制但仍然得到"算术溢出错误将varchar转换为数据类型numeric"

时间:2017-03-29 12:06:45

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

我有一个问题,我从临时表中选择,但我转换为浮点,以确保值是正确的。但是当我运行查询时,我收到以下错误

  

将varchar转换为数据类型numeric

的算术溢出错误

即使我在这里没有选择varchar列,也是声明:

declare @AccountNumber varchar(25)
declare @FromDate datetime
declare @ToDate datetime

set @AccountNumber='ROSECALL'
set @FromDate='1980/01/01'
set @ToDate='2017/01/01'

Create table #TransF(RowId int NOT NULL identity(1,1), ValueDate datetime, MovementType varchar(50), Remarks varchar(100), Dr Decimal(19,2),
     Cr Decimal(19,2), Balance float)

insert into #TransF (ValueDate, MovementType, Remarks, Dr, Cr) 
values (@FromDate, 'Opening Balance', '',0,0);

insert into #TransF (ValueDate, MovementType, Remarks, Dr, Cr)
Select MoveF.ValueDate, MCodeF.Description,

MoveF.Comment,
(select (
           case (MoveF.Action)
             when 'W' then MoveF.MoveAmount
               else 0
           end
         )),
(select (
           case (MoveF.Action)
             when 'D' then -MoveF.MoveAmount
               else 0
           end
         ))
From CashMovements as MoveF,
     MovementCodes as MCodeF
Where 
     ( MoveF.AccountNumber=@AccountNumber) and        
        MCodeF.MovementCode=MoveF.MovementCode and
      MoveF.ValueDate>=@FromDate and
      MoveF.ValueDate<=@ToDate
Order By MoveF.ValueDate

insert into #TransF (ValueDate, MovementType, Remarks, Dr, Cr) 
values (@ToDate, 'Closing Balance','',0,0);


Update #TransF set Balance=
(select (isnull((Select SUM(MoveF.MoveAmount)
From CashMovements as MoveF
Where 
      MoveF.AccountNumber=@AccountNumber and
      MoveF.ValueDate<@FromDate and
      MoveF.DebitCredit='W'),0)
-      
isnull((Select SUM(MoveF.MoveAmount)
From CashMovements as MoveF
Where (MoveF.AccountNumber=@AccountNumber) and
      MoveF.ValueDate<@FromDate and
      MoveF.DebitCredit='DC'),0)))
where RowId=1

SELECT 2 AS RowId INTO #RowID
while ((SELECT TOP 1 RowId FROM #RowID)<=(select count(1) from #TransF))
begin
  Update #TransF set Balance=((select Balance from #TransF where RowId=(SELECT TOP 1 RowId FROM #RowID)-1)+Dr-Cr) where RowId=(SELECT TOP 1 RowId FROM #RowID)
  UPDATE #RowID SET RowId = (SELECT TOP 1 RowId FROM #RowID) +1;
end;


Select TransF.ValueDate as 'Value Date', 
       TransF.MovementType as 'Movement Type', 
       TransF.Remarks as 'Remarks',
       Case when ((replace(CONVERT(Decimal(19,2) , TransF.DR, 1),',',' ') = 0.00))then 0 else (replace(CONVERT(Decimal(19,2), TransF.Dr, 1),',',' ')) end  as 'Debit',
         Case when ((replace(CONVERT(Decimal(19,2), TransF.CR, 1),',',' ') = 0.00))then 0 else (replace(CONVERT(Decimal(19,2),TransF.CR, 1),',',' ')) end  as 'Credit',

          Cast(TransF.Balance as decimal)as 'Balance'
            From #TransF as TransF
                Order By TransF.RowId
                Drop table #TransF
                DROP TABLE #RowID

我得到的错误:

  

Msg 8115,Level 16,State 8,Line 67
  将varchar转换为数据类型numeric的算术溢出错误。

魔术发生在最后一个选择但是它在第二个案例声明上的错误,我知道我可以使用IIF,但我只有SQL Server 2008

1 个答案:

答案 0 :(得分:0)

您可能会尝试在decimal上的float之后转换为replace()varchar

 , case 
  when CONVERT(decimal(16, 2),replace(TransF.DR, ',', ' ')) = 0.00
   then 0
  else  CONVERT(decimal(16, 2),replace(TransF.DR, ',', ' '))
  end as 'Debit'
 , case 
  when  CONVERT(float,replace(TransF.CR, ',', ' '))= 0.00
   then 0
  else CONVERT(float,replace(TransF.CR, ',', ' '))
  end as 'Credit'
 ,

您可能需要使用更大的decimal。试试decimal(19,2)decimal(28,2)decimal(38,2)

rextester演示:http://rextester.com/HUNUF16216

declare @f float = '123456789012345.123';

select convert(decimal(19,2),@f); -- works

select convert(decimal(16,2),@f); -- overflow error

参考: