update VS4TProduct set tpdtlastcost=(select top 1 TIMCunitcost FROM VS4TInventoryLOT INNER JOIN (VS4TInventory INNER JOIN VS4TInventoryMB ON VS4TInventory.TInvNo = VS4TInventoryMB.TInMTInvNo) ON (VS4TInventoryLOT.TIMCLotDate = VS4TInventoryMB.TInMLotDate) AND (VS4TInventoryLOT.TIMCLotNo = VS4TInventoryMB.TInMLotNo) AND (VS4TInventoryLOT.TIMCSerialNo = VS4TInventoryMB.TInMSerialNo) AND (VS4TInventoryLOT.TIMCCustom1 = VS4TInventoryMB.TInMCustom1) AND (VS4TInventoryLOT.TIMCTPdtCode = VS4TInventoryMB.TInMTPdtCode)
 where tinmstatus=1 and tinmamouquantity>0 and tinvapprovestatus=1 and tinvcancelstatus=0 and
 tinmtintalias like 'LOTIN%' ORDER BY TInvDate DESC, TInvApproveDate  desc) where tpdtlastcost=0
 GO
 update VS4TProduct set tpdtlastcost=TPdtFirstCostInStk where tpdtlastcost=0 or tpdtlastcost is null
GO