update tgn
set tgn.[NextNumber] = tgnr.[Number]
from [IES].[T_GEN_NUMBERS] tgn
join [IES].[T_GEN_NUMBERS_R] tgnr
on tgn.[EntityID] = tgnr.[EntityID] 
	and tgn.[NumberName] = 'SchetNumber-' + tgnr.[NumberName] + '-' + cast([Year] as varchar(4));

insert into [IES].[T_GEN_NUMBERS]
([GenNumbersID], [EntityID], [NumberName], [NextNumber])
select [GennumbersrID], [EntityID], 'SchetNumber-' + [NumberName] + '-' + cast([Year] as varchar(4)), Number
from [IES].[T_GEN_NUMBERS_R] tgnr
where not exists (
	select 1 from [IES].[T_GEN_NUMBERS] tgn
	where tgn.[EntityID] = tgnr.[EntityID] 
	and tgn.[NumberName] = 'SchetNumber-' + tgnr.[NumberName] + '-' + cast([Year] as varchar(4))
);