update tt_com set chavea = (select max(ven.chavea) from tt_ven ven inner join tt_tra tra on tra.filive = ven.codfil and tra.seqive = ven.sequen where tra.filico = tt_com.codfil and tra.seqico = tt_com.sequen) where tt_com.chavea is null and tt_com.flgest <> 'D' and exists (select 1 as ok from tt_ico ico inner join tt_tra tra on tra.filico = ico.codfil and tra.seqico = ico.sequen and tra.numico = ico.numite where ico.codfil = tt_com.codfil and ico.sequen = tt_com.sequen);