if exists (select * from tempdb..sysobjects where type='u' and name='forcurrentsyz')
drop table tempdb..forcurrentsyz
go
select a.cwhcode,a.cinvcode,sum(iquantity) as iquantity,
sum(a.iquantity/Inventory.iInvExchRate) as inum
into tempdb..forcurrentsyz from
(
select cwhcode,cinvcode,iaInquantity as iquantity from ia_subsidiary where cvoutype='34'
union all
select cwhcode,cinvcode,( case when brdflag=1 then iquantity else -1*(iquantity) end) as iquantity
from rdrecord left join rdrecords on rdrecord.id=rdrecords.id
) a left join Inventory
on a.cinvcode=Inventory.cinvcode
group by a.cwhcode,a.cinvcode
go
delete from currentstock
Go
insert into currentstock (cwhcode,cinvcode,iquantity,inum) select cwhcode,cinvcode,iquantity,inum from tempdb..forcurrentsyz
go