delete cmf
from VCLib.T_CLIENT_MODULE_FORM cmf
	join VCLib.T_CLIENT_MODULES cm on cm.ClientModuleID = cmf.ClientModules
where cm.ModuleType in ('Custom_DnDoctorClientModule', 'Custom_DispDoctorClientModule')
GO

insert into VCLib.T_ROLE_CLIENT_MODULES(ClientModuleID,LoadOrder,RoleClientModuleID,RoleID)
select cmn.ClientModuleID, rcm.LoadOrder, NEWID(), rcm.RoleID
from VCLib.T_ROLE_CLIENT_MODULES rcm
join VCLib.T_CLIENT_MODULES cm on cm.ClientModuleID = rcm.ClientModuleID
join VCLib.T_CLIENT_MODULES cmn on cmn.ModuleType = 'Custom_' + cm.ModuleType
where cm.ModuleType in ( 'RecommendationClientModule', 'DispBaseClientModule', 'DnBaseClientModule', 'FondClientModule' )
GO

delete rcm1
from VCLib.T_ROLE_CLIENT_MODULES rcm1
where exists
	(
		select *
		from VCLib.T_ROLE_CLIENT_MODULES rcm2
		where rcm1.ClientModuleID = rcm2.ClientModuleID
				and
			rcm1.RoleID = rcm2.RoleID
				and
			rcm1.RoleClientModuleID < rcm2.RoleClientModuleID
	)
GO
