declare @exportServiceID uniqueidentifier;

select  top 1 @exportServiceID = ExportRecordID from IES.T_EXPORT_RECORD where ProcessName = 'UnloadActsReportPeriod';

insert into IES.T_EXPORT_RECORD_SERVICE_ROLES
(ExportRecordServiceRolesID, ExportRecordService, Roles)
select newid(),  @exportServiceID, tr.RoleID
from VCLib.T_BASE_FEATURE_SETTINGS tbfs
join VCLib.T_ROLE_CLIENT_MODULES trcm on tbfs.RoleClientModules = trcm.RoleClientModuleID
join VCLib.T_ROLES tr on tr.RoleID = trcm.RoleID
join VCLib.T_CLIENT_MODULE_FEATURE tcmf on tcmf.ClientModuleFeatureID = tbfs.ClientModuleFeature
where tcmf.[Name] = 'UnloadActsReportPeriod';

select  top 1 @exportServiceID = ExportRecordID from IES.T_EXPORT_RECORD where ProcessName = 'UnloadSMOActsRB';

insert into IES.T_EXPORT_RECORD_SERVICE_ROLES
(ExportRecordServiceRolesID, ExportRecordService, Roles)
select newid(),  @exportServiceID, tr.RoleID
from VCLib.T_BASE_FEATURE_SETTINGS tbfs
join VCLib.T_ROLE_CLIENT_MODULES trcm on tbfs.RoleClientModules = trcm.RoleClientModuleID
join VCLib.T_ROLES tr on tr.RoleID = trcm.RoleID
join VCLib.T_CLIENT_MODULE_FEATURE tcmf on tcmf.ClientModuleFeatureID = tbfs.ClientModuleFeature
where tcmf.[Name] = 'UnloadSMOActsRBPeriod';

delete bfs 
from VCLib.T_BASE_FEATURE_SETTINGS bfs
join VCLib.T_CLIENT_MODULE_FEATURE cmf on bfs.ClientModuleFeature = cmf.ClientModuleFeatureID
join VCLib.T_CLIENT_MODULES cm on cmf.ClientModules = cmf.ClientModules
where cm.ModuleType = 'ActsUnloadClientModule'  and cm.[Schema] = 'IES'
		and
	cmf.Name in ('UnloadActsReportPeriod', 'UnloadSMOActsRBPeriod');

delete cmf
from VCLib.T_CLIENT_MODULES cm
join VCLib.T_CLIENT_MODULE_FEATURE cmf on cmf.ClientModules = cm.ClientModuleID
where cm.ModuleType = 'ActsUnloadClientModule'  and cm.[Schema] = 'IES'
		and
	cmf.Name in ('UnloadActsReportPeriod', 'UnloadSMOActsRBPeriod');