insert into VCLib.T_GEN_NUMBERS (GenNumbersID, EntityID, NumberName, NextNumber)
select newid(), gen.EntityID, gen.NumberName, max(gen.NextNumber)
from AKUZ.T_GEN_NUMBERS gen
join VCLib.T_ENTITIES ent on ent.EntityID = gen.EntityID
where ent.[Schema] in ('STOR', 'PHARM', 'VMP') 
group by gen.NumberName, gen.EntityID