declare @AdministratorClientModuleID uniqueidentifier = (select top(1) ClientModuleID from vclib.T_CLIENT_MODULES cm where cm.ModuleType = 'AdministratorClientModule' and cm.[Schema] = 'VCLib')
declare @AdminClientModuleID uniqueidentifier = (select top(1) ClientModuleID from vclib.T_CLIENT_MODULES cm where cm.ModuleType = 'AdminClientModule' and cm.[Schema] = 'VCLib')

--перепривязка фич на модуль AdministratorClientModule
update cmf
set cmf.ClientModules = @AdministratorClientModuleID
--select *
from vclib.T_CLIENT_MODULE_FEATURE cmf
inner join vclib.T_CLIENT_MODULES cm
	on cm.ClientModuleID = cmf.ClientModules
	and cm.ModuleType = 'AdminClientModule'
	and cm.[Schema] = 'VCLib'

if OBJECT_ID('tempdb..#role_client_modules') is not null
	drop table #role_client_modules

select rcm.RoleClientModuleID, rcm.ClientModuleID, rcm.RoleID, rcm.LoadOrder
into #role_client_modules
from VCLib.T_ROLE_CLIENT_MODULES rcm
where rcm.ClientModuleID = @AdminClientModuleID

alter table #role_client_modules add RoleClientModuleID_New uniqueidentifier

update trcm
set trcm.RoleClientModuleID_New = rcm.RoleClientModuleID
from #role_client_modules trcm
inner join vclib.T_ROLE_CLIENT_MODULES rcm 
	on rcm.ClientModuleID = @AdministratorClientModuleID 
	and rcm.RoleID = trcm.RoleID

update #role_client_modules
set RoleClientModuleID_New = newid()
where RoleClientModuleID_New is null

--ролям, к которым привязан AdminClientModule и не привязан AdministratorClientModule, привязать AdministratorClientModule
insert into vclib.T_ROLE_CLIENT_MODULES (RoleClientModuleID, ClientModuleID, RoleID, LoadOrder)
select trcm.RoleClientModuleID_New as RoleClientModuleID
	, @AdministratorClientModuleID as ClientModuleID
	, trcm.RoleID
	, trcm.LoadOrder
from #role_client_modules trcm
where not exists (select * from vclib.T_ROLE_CLIENT_MODULES rcm where rcm.RoleClientModuleID = trcm.RoleClientModuleID_New)

if OBJECT_ID('tempdb..#base_feature_settings') is not null
	drop table #base_feature_settings

--сохраняем все связки фич с RoleClientModule модуля AdminClientModuleID
select bfs.BaseFeatureSettingsID, bfs.ClientModuleFeature, bfs.RoleClientModules, bfs.type_, trcm.RoleClientModuleID_New as RoleClientModuleID_New
into #base_feature_settings
from vclib.T_BASE_FEATURE_SETTINGS bfs
inner join #role_client_modules trcm
	on trcm.RoleClientModuleID = bfs.RoleClientModules
inner join vclib.T_CLIENT_MODULES cm
	on cm.ClientModuleID = trcm.ClientModuleID
	and cm.ClientModuleID = @AdminClientModuleID

alter table #base_feature_settings add BaseFeatureSettingsID_New uniqueidentifier

update tbfs
set tbfs.BaseFeatureSettingsID_New = bfs.BaseFeatureSettingsID
from #base_feature_settings tbfs
inner join vclib.T_BASE_FEATURE_SETTINGS bfs
	on bfs.ClientModuleFeature = tbfs.ClientModuleFeature
	and bfs.RoleClientModules = tbfs.RoleClientModuleID_New

update #base_feature_settings
set BaseFeatureSettingsID_New = newid()
where BaseFeatureSettingsID_New is null

--создаем связки фич с новыми RoleClientModule принадлежащие AdministratorClientModuleID
insert into vclib.T_BASE_FEATURE_SETTINGS (BaseFeatureSettingsID, ClientModuleFeature, RoleClientModules, type_)
select tbfs.BaseFeatureSettingsID_New
	, tbfs.ClientModuleFeature
	, tbfs.RoleClientModuleID_New
	, tbfs.type_
from #base_feature_settings tbfs
where not exists (select * from vclib.T_BASE_FEATURE_SETTINGS bfs where bfs.BaseFeatureSettingsID = tbfs.BaseFeatureSettingsID_New)

--перепривязываем связки модулей с новостями с модуля AdminClientModuleID на AdministatorClientModuleID
insert into vclib.T_NEWS_BOX_LINK_MODULE (NewsBoxLinkModuleID, NewsBox, ClientModules)
select NEWID()
	, NewsBox
	, ClientModules
from vclib.T_NEWS_BOX_LINK_MODULE nb
where nb.ClientModules = @AdminClientModuleID
and not exists (select * from vclib.T_NEWS_BOX_LINK_MODULE nbl where nbl.NewsBox = nb.NewsBox and nbl.ClientModules = @AdministratorClientModuleID)

delete nb
from vclib.T_NEWS_BOX_LINK_MODULE nb
where nb.ClientModules = @AdminClientModuleID

--перепривязываем связки модулей с документами с модуля AdminClientModuleID на AdministatorClientModuleID
insert into vclib.T_ADD_DOCUMENT_LINK_MODULE (AddDocumentLinkModuleID, AddingDocument, ClientModules)
select NEWID()
	, AddingDocument
	, ClientModules
from vclib.T_ADD_DOCUMENT_LINK_MODULE nb
where nb.ClientModules = @AdminClientModuleID
and not exists (select * from vclib.T_ADD_DOCUMENT_LINK_MODULE nbl where nbl.AddingDocument = nb.AddingDocument and nbl.ClientModules = @AdministratorClientModuleID)

delete nb
from vclib.T_ADD_DOCUMENT_LINK_MODULE nb
where nb.ClientModules = @AdminClientModuleID

--удаляем старые связки фич с RoleClientModule модуля AdminClientModuleID
delete bfs
from vclib.T_BASE_FEATURE_SETTINGS bfs
inner join #role_client_modules trcm
	on trcm.RoleClientModuleID = bfs.RoleClientModules
inner join vclib.T_CLIENT_MODULES cm
	on cm.ClientModuleID = trcm.ClientModuleID
	and cm.ClientModuleID = @AdminClientModuleID

--удаляем старые связки ролей с клиентским модулем AdminClientModuleID
delete rcm
from VCLib.T_ROLE_CLIENT_MODULES rcm
where rcm.ClientModuleID = @AdminClientModuleID

delete cm
from VCLib.T_CLIENT_MODULES cm
where cm.ModuleType = 'AdminClientModule' and cm.[Schema] = 'VCLib'