Wednesday, August 21, 2013

While Loop with temp table in SQL Server


declare @i int
set @i=1
WHILE (@i < 10)
Begin
set @i=@i+1
--select  '1'

drop table #temp32
select  * into  #temp32   from #temp31 
update #temp32 set RemainingLife=(RemainingLife-1), Accum=Accum+DeprPerMonth, NetBookValue=cast(NetCost+(Accum+DeprPerMonth) as decimal(15,2)),PastLife=1+PastLife,[month]=@i --where RemainingLife>0
update #temp32 set DeprPerMonth=0 --where RemainingLife>0
-- for IN N I
update  #temp32  set DeprPerMonth= -(isnull(NetCost,0)+ isnull(Accum,0))
where DeprMeth<>'00' and (cast((isnull(NetCost,0)/AssetLife)* -1 *(PastLife)- isnull(Accum,0) as decimal(15,2))-(isnull(NetCost,0)+ isnull(Accum,0)))>0
 
update  #temp32  set DeprPerMonth= cast((NetCost/AssetLife)* -1 *(PastLife)-isnull(Accum,0) as decimal(15,2))
where DeprMeth<>'00' and (cast((isnull(NetCost,0)/AssetLife)* -1 *(PastLife)- isnull(Accum,0) as decimal(15,2))-(isnull(NetCost,0)+ isnull(Accum,0)))<0
 
update  #temp32  set DeprPerMonth=0 where NetBookValue<=0 
update  #temp32  set Accum=0,NetBookValue=0,DeprPerMonth=0 where  Accum is null
update  #temp32  set PastLife=AssetLife where  PastLife>AssetLife
update  #temp32  set NetBookValue=NetCost where  Accum =0
-- IN '' 'I'
update #temp32 set DeprPerMonth =
(case when (cast((isnull(NetCost,0)/AssetLife)* -1 *(PastLife-1)- isnull(Accum,0) as decimal(15,2))-(isnull(NetCost,0)+ isnull(Accum,0)))>0 then -(isnull(NetCost,0)+ isnull(Accum,0))
Else cast((NetCost/AssetLife)* -1 *(PastLife-1)-isnull(Accum,0) as decimal(15,2))  end )*-1
where  DeprMeth='IN' and DeprInfo='' and CompMeth='I'  
-- 01 'N' 'I'
-- For disposable vale
update #temp32 set DeprPerMonth =cast(isnull(NetBookValue,0)as decimal(15,2))*-1 where  DeprMeth='01' and DeprInfo='N' and CompMeth='P'   and RemainingLife=-1 
update #temp32 set DeprPerMonth =case when RemainingLife+1 >0 then -1*(NetBookValue/(RemainingLife+1)) else 0 end where  DeprMeth='01' and DeprInfo='N' and CompMeth='P'  and AssetLife=84 
and   RemainingLife<>-1 
update #temp32 set DeprPerMonth =cast(isnull(NetCost/AssetLife,0)as decimal(15,2))*-1   where  DeprMeth='01' and DeprInfo='N' and CompMeth='P' and   AssetLife<>84 and NetBookValue>0  and   RemainingLife<>-1 
update #temp32 set DeprPerMonth =cast(isnull(NetBookValue,0)as decimal(15,2))*-1   where  DeprMeth='01' and DeprInfo='N' and CompMeth='P' and   AssetLife<>84 and NetBookValue>0 and   RemainingLife<>-1 
and NetCost/AssetLife>NetBookValue

-- Make it zero after straight line deprication calculation
update  #temp32  set RemainingLife=0 where  RemainingLife<0
--NB N P
update #temp32 set DeprPerMonth =case when RemainingLife+1 >0 then -1*(NetBookValue/(RemainingLife+1)) else 0 end where  DeprMeth='NB' and DeprInfo='N' and CompMeth='P' 
update #temp32 set NetBookValue=cast(NetBookValue as decimal(15,2)),Accum=cast(Accum as decimal(15,2)),DeprPerMonth=cast(DeprPerMonth as decimal(15,2))

drop table #temp31
select  * into  #temp31   from #temp32
insert into testTab  select  *  from #temp32

CONTINUE
end 

No comments: