declare @fitemid int,@finterid int,@fprice decimal(18,10),@fdate datetime,@count int
declare my_cursor cursor for
select distinct fitemid from poorderentry where fprice>0
open my_cursor
fetch next from my_cursor into @fitemid
while @@fetch_status=0
begin
select top 1 @fprice=b.fprice,@fdate='2019-01-29' from poorder a inner join poorderentry b on a.finterid=b.finterid
where b.fitemid=@fitemid and b.fauxprice>0 order by a.fdate desc
update t_icitemmaterial set fplanprice=@fprice where fitemid=@fitemid
select @count=count(fitemid) from t_icitemcustom where fitemid=@fitemid
if @count>0
begin
update t_icitemcustom set f_102=@fdate where fitemid=@fitemid
end
else
begin
insert into t_icitemcustom (fitemid,f_102)values(@fitemid,@fdate)
end
fetch next from my_cursor into @fitemid
end
close my_cursor
deallocate my_cursor