insert into ies.T_DICTIONARY_LIST(DictionaryListID, DictionaryType, Code, Entities, Name)
select newid(), (select top 1 DictionaryTypeID from ies.T_DICTIONARY_TYPE where code = '2'), SUBSTRING(t1.EntName, 0, 5), t1.EntityID, 'F032 - Реестр МО, осуществляющих деятельность в сфере ОМС'
from vclib.T_ENTITIES t1
where entname = 'F032_TRMO'

insert into ies.T_DICTIONARY_LIST(DictionaryListID, DictionaryType, Code, Entities, Name)
select newid(), (select top 1 DictionaryTypeID from ies.T_DICTIONARY_TYPE where code = '2'), SUBSTRING(t1.EntName, 0, 5), t1.EntityID, 'F042 - Реестр экспертов качества МП'
from vclib.T_ENTITIES t1
where entname = 'F042_TREKMP'

insert into ies.T_DICTIONARY_LIST_ROLES(DictionaryListRolesID, DictionaryList, Roles)
select newid(), aa.DictionaryListID, t2.Roles
from ies.T_DICTIONARY_LIST t1
join ies.T_DICTIONARY_LIST_ROLES t2 on (t1.DictionaryListID = t2.DictionaryList)
cross apply(select dl.DictionaryListID from ies.T_DICTIONARY_LIST dl 
		where dl.code in ('F032', 'F042') 
			and not exists (select 1/0 from ies.T_DICTIONARY_LIST_ROLES dlr where dlr.DictionaryList = dl.DictionaryListID)) aa
where t1.code = 'V021'