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
, 'F031 - Единый реестр медицинских организаций'
from vclib.T_ENTITIES t1
where entname = 'F031_ERMO'

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
, 'F033 - Структурные подразделения МО осуществляющие деятельность в сфере ОМС'
from vclib.T_ENTITIES t1
where entname = 'F033_SPMO'

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
, 'F034 - Справочник видов, услови и профилей МП, оказываемой МО'
from vclib.T_ENTITIES t1
where entname = 'F034_VUP'

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
, 'F035 - Справочник учредителей МО'
from vclib.T_ENTITIES t1
where entname = 'F035_UMO'

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
, 'F036 - Справочник руководителей МО'
from vclib.T_ENTITIES t1
where entname = 'F036_RukMO'

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
, 'F037 - Справочник лицензий МО'
from vclib.T_ENTITIES t1
where entname = 'F037_LicMO'

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
, 'F038 - Справочник адресов оказания МП'
from vclib.T_ENTITIES t1
where entname = 'F038_ADDRMP'

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 ('F031', 'F033', 'F034', 'F035', 'F036', 'F037', 'F038') 
			and not exists (select 1/0 from ies.T_DICTIONARY_LIST_ROLES dlr where dlr.DictionaryList = dl.DictionaryListID)) aa
where t1.code = 'F003'

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F031', 'sp_F031ErmoLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F031');

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F033', 'sp_F033SpmoLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F033');

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F034', 'sp_F034VupLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F034');

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F035', 'sp_F035UmoLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F035');

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F036', 'sp_F036RukMoLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F036');

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F037', 'sp_F037LicMoLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F037');

insert into ies.T_NSI_UPDATE(DictionaryName, ProcedureName, NsiUpdateID)
select 'F038', 'sp_F038AddrmpLoad', newid()
where not exists (select 1 from ies.T_NSI_UPDATE where DictionaryName = 'F038');