You could try fetching the max of the existing type id and add a row number to that. Something like
insert into GeneralTypes (TypeId, TypeName)
select Row_Number() over (order by TypeName)
+ coalesce((select max(b.TypeId)
from GeneralTypes b), 0),
TypeName
from #tempGeneral a
where not exists (select 1
from GeneralTypes b
where b.TypeName = a.TypeName)
However, I would reconsider using an identity or a sequence for the type id since this would eliminate the need to generate unique keys. Another things you must keep in mind:
- If a row is deleted, that id is gone and the number will not be reused. In other words you have a hole in your numbering
- Depending on other facctors of the design you may have key collisions if two or more sessions ad to the table add the same time.