INSERT INTO VCLib.T_GEN_NUMBERS (GenNumbersID, EntityID, NumberName, NextNumber)
SELECT uuid_generate_v4(), gen.EntityID, gen.NumberName, max(gen.NextNumber)
FROM APS.T_GEN_NUMBERS gen
JOIN VCLib.T_ENTITIES ent ON ent.EntityID = gen.EntityID
WHERE ent.Schema IN ('STOR', 'PHARM', 'APS') 
GROUP BY gen.NumberName, gen.EntityID;