merge two table and generate code at the same time

0

I want to update my table and at the same time generate unique code for it my destination table contain of code , province code , province name , city code , city name like : 61610001, 61, Tehran, 610001, Firooz 61610002, 61, Tehran, 610002, Damavand 52520001, 52, Kerman, 520001, Bam every day I receive an excel than contain Of a table of new province , city (just name ) that I should insert with a correct code for example if I have a new province my query return max of province code and generate code and city code from that code .and if my province is exists my query insert a row witch province code already exist and generate the maximum code of its level I should mention that a new province is rarely occurred but I want to have perfect code and I generate a code witch is correct when just on row is new but in two records it returns errors