删除Management Data Warehouse (MDW) job失败 推荐
2014-01-08 10:38
537 查看
最近在清理一些不用的Job,发现几个跟MDW有关的。虽然Job已经被Disable, 但是没有被删除。尝试删除出现下面的错误:
The DELETE statement conflicted with the REFERENCE constraint "FK_syscollector_collection_sets_collection_sysjobs". The conflict occurred in database "msdb", table "dbo.syscollector_collection_sets_internal", column 'collection_job_id'.The statement has been terminated. (.Net SqlClient Data Provider)
查了一些文档发现这个问题在2008/2008 R2中都存在,只能Disable但是无法删除。找到了一篇文章Remove associated data collector jobs提供了代码去删除MDW的相关job和对象.虽然文章提到只能在测试环境中运行这个脚本,但是这个脚本不会对其他应用产生影响。
USE MSDBGO-- Disable constraintsALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Delete data collector jobsDECLARE @job_id uniqueidentifierDECLARE datacollector_jobs_cursor CURSORLOCALFORSELECT collection_job_id AS job_id FROMsyscollector_collection_setsWHERE collection_job_id ISNOTNULLUNIONSELECT upload_job_id AS job_id FROMsyscollector_collection_setsWHERE upload_job_id ISNOTNULL OPEN datacollector_jobs_cursorFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idWHILE (@@fetch_status= 0)BEGINIFEXISTS(SELECTCOUNT(job_id)FROM sysjobs WHERE job_id = @job_id )BEGINDECLARE @job_name sysnameSELECT @job_name = name from sysjobs WHERE job_id = @job_idPRINT'Removing job '+ @job_nameEXEC dbo.sp_delete_job@job_id=@job_id, @delete_unused_schedule=0ENDFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idENDCLOSE datacollector_jobs_cursorDEALLOCATE datacollector_jobs_cursor -- Enable Constraints backALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Disable trigger on syscollector_collection_sets_internalEXEC('DISABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- Set collection sets as not running stateUPDATE syscollector_collection_sets_internalSET is_running = 0 -- Update collect and upload jobs as nullUPDATE syscollector_collection_sets_internalSET collection_job_id =NULL, upload_job_id =NULL -- Enable back trigger on syscollector_collection_sets_internalEXEC('ENABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- re-set collector config storeUPDATE syscollector_config_store_internalSET parameter_value = 0WHERE parameter_name IN('CollectorEnabled') UPDATE syscollector_config_store_internalSET parameter_value =NULLWHERE parameter_name IN('MDWDatabase','MDWInstance') -- Delete collection set logsDELETEFROM syscollector_execution_log_internal GO运行之后会看到下面的结果:
Removing job collection_set_3_collectionRemoving job collection_set_2_uploadRemoving job collection_set_1_noncached_collect_and_uploadRemoving job collection_set_2_collectionRemoving job collection_set_3_upload
可以看到跟MDW有关的job已经被移除。另外提一下在2012中微软提供了一个新的系统存储过程sp_syscollector_delete_collection_set,可以删除自定义的MDW job和对象,其实看下这个存储过程的代码跟上面的差不多。
CREATEPROC [dbo].[sp_syscollector_cleanup_collector]@collection_set_id INT=NULLASBEGINIF (@collection_set_id ISNOTNULL)BEGINDECLARE @retVal intEXEC @retVal = dbo.sp_syscollector_verify_collection_set@collection_set_id OUTPUTIF (@retVal <> 0)BEGINRETURN (1)ENDEND DECLARE @TranCounter INTSET @TranCounter =@@TRANCOUNTIF (@TranCounter > 0)SAVETRANSACTION tran_cleanup_collection_setELSEBEGINTRANSACTION BEGINTRY-- changing isolation level to repeatable to avoid any conflicts that may happen-- while running this stored procedure and sp_syscollector_start_collection_set concurrentlySETTRANSACTIONISOLATIONLEVELREPEATABLEREAD -- Security check (role membership)IF (NOT(ISNULL(IS_MEMBER(N'dc_admin'), 0)= 1)ANDNOT(ISNULL(IS_MEMBER(N'db_owner'), 0)= 1))BEGINREVERTRAISERROR(14677,-1,-1,'dc_admin')RETURN (1)END -- Disable constraints-- this is done to make sure that constraint logic does not interfere with cleanup processALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Delete data collector jobsDECLARE @job_id uniqueidentifierDECLARE datacollector_jobs_cursor CURSORLOCALFORSELECT collection_job_id AS job_id FROMsyscollector_collection_setsWHERE collection_job_id ISNOTNULLAND( collection_set_id = @collection_set_id OR @collection_set_id ISNULL)UNIONSELECT upload_job_id AS job_id FROMsyscollector_collection_setsWHERE upload_job_id ISNOTNULLAND( collection_set_id = @collection_set_id OR @collection_set_id ISNULL) OPEN datacollector_jobs_cursorFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idWHILE (@@fetch_status= 0)BEGINIFEXISTS(SELECTCOUNT(job_id)FROM sysjobs WHERE job_id = @job_id )BEGINDECLARE @job_name sysnameSELECT @job_name = name from sysjobs WHERE job_id = @job_idPRINT'Removing job '+ @job_nameEXEC dbo.sp_delete_job@job_id=@job_id, @delete_unused_schedule=0ENDFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idENDCLOSE datacollector_jobs_cursorDEALLOCATE datacollector_jobs_cursor -- Enable Constraints backALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Disable trigger on syscollector_collection_sets_internal-- this is done to make sure that trigger logic does not interfere with cleanup processEXEC('DISABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- Set collection sets as not running state and update collect and upload jobs as nullUPDATE syscollector_collection_sets_internalSET is_running = 0,collection_job_id =NULL,upload_job_id =NULLWHERE (collection_set_id = @collection_set_id OR @collection_set_id ISNULL) -- Enable back trigger on syscollector_collection_sets_internalEXEC('ENABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- re-set collector config store if there is no enabled collectorDECLARE @counter INTSELECT @counter=COUNT(is_running)FROM syscollector_collection_sets_internal WHERE is_running = 1 IF (@counter = 0)BEGINUPDATE syscollector_config_store_internalSET parameter_value = 0WHERE parameter_name IN('CollectorEnabled'); UPDATE syscollector_config_store_internalSET parameter_value =NULLWHERE parameter_name IN('MDWDatabase','MDWInstance')END -- Delete collection set logsDELETEFROM syscollector_execution_log_internalWHERE (collection_set_id = @collection_set_id OR @collection_set_id ISNULL) IF (@TranCounter = 0)BEGINCOMMITTRANSACTIONENDRETURN(0)ENDTRYBEGINCATCHIF (@TranCounter = 0 ORXACT_STATE()=-1)ROLLBACKTRANSACTIONELSEIF (XACT_STATE()= 1)ROLLBACKTRANSACTION tran_cleanup_collection_set DECLARE @ErrorMessageNVARCHAR(4000);DECLARE @ErrorSeverityINT;DECLARE @ErrorStateINT;DECLARE @ErrorNumberINT;DECLARE @ErrorLineINT;DECLARE @ErrorProcedure NVARCHAR(200);SELECT @ErrorLine =ERROR_LINE(),@ErrorSeverity =ERROR_SEVERITY(),@ErrorState =ERROR_STATE(),@ErrorNumber =ERROR_NUMBER(),@ErrorMessage =ERROR_MESSAGE(),@ErrorProcedure =ISNULL(ERROR_PROCEDURE(),'-');RAISERROR (14684, @ErrorSeverity,-1 , @ErrorNumber, @ErrorSeverity, @ErrorState, @ErrorProcedure, @ErrorLine, @ErrorMessage);RETURN (1)ENDCATCHEND
The DELETE statement conflicted with the REFERENCE constraint "FK_syscollector_collection_sets_collection_sysjobs". The conflict occurred in database "msdb", table "dbo.syscollector_collection_sets_internal", column 'collection_job_id'.The statement has been terminated. (.Net SqlClient Data Provider)
查了一些文档发现这个问题在2008/2008 R2中都存在,只能Disable但是无法删除。找到了一篇文章Remove associated data collector jobs提供了代码去删除MDW的相关job和对象.虽然文章提到只能在测试环境中运行这个脚本,但是这个脚本不会对其他应用产生影响。
USE MSDBGO-- Disable constraintsALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Delete data collector jobsDECLARE @job_id uniqueidentifierDECLARE datacollector_jobs_cursor CURSORLOCALFORSELECT collection_job_id AS job_id FROMsyscollector_collection_setsWHERE collection_job_id ISNOTNULLUNIONSELECT upload_job_id AS job_id FROMsyscollector_collection_setsWHERE upload_job_id ISNOTNULL OPEN datacollector_jobs_cursorFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idWHILE (@@fetch_status= 0)BEGINIFEXISTS(SELECTCOUNT(job_id)FROM sysjobs WHERE job_id = @job_id )BEGINDECLARE @job_name sysnameSELECT @job_name = name from sysjobs WHERE job_id = @job_idPRINT'Removing job '+ @job_nameEXEC dbo.sp_delete_job@job_id=@job_id, @delete_unused_schedule=0ENDFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idENDCLOSE datacollector_jobs_cursorDEALLOCATE datacollector_jobs_cursor -- Enable Constraints backALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Disable trigger on syscollector_collection_sets_internalEXEC('DISABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- Set collection sets as not running stateUPDATE syscollector_collection_sets_internalSET is_running = 0 -- Update collect and upload jobs as nullUPDATE syscollector_collection_sets_internalSET collection_job_id =NULL, upload_job_id =NULL -- Enable back trigger on syscollector_collection_sets_internalEXEC('ENABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- re-set collector config storeUPDATE syscollector_config_store_internalSET parameter_value = 0WHERE parameter_name IN('CollectorEnabled') UPDATE syscollector_config_store_internalSET parameter_value =NULLWHERE parameter_name IN('MDWDatabase','MDWInstance') -- Delete collection set logsDELETEFROM syscollector_execution_log_internal GO运行之后会看到下面的结果:
Removing job collection_set_3_collectionRemoving job collection_set_2_uploadRemoving job collection_set_1_noncached_collect_and_uploadRemoving job collection_set_2_collectionRemoving job collection_set_3_upload
可以看到跟MDW有关的job已经被移除。另外提一下在2012中微软提供了一个新的系统存储过程sp_syscollector_delete_collection_set,可以删除自定义的MDW job和对象,其实看下这个存储过程的代码跟上面的差不多。
CREATEPROC [dbo].[sp_syscollector_cleanup_collector]@collection_set_id INT=NULLASBEGINIF (@collection_set_id ISNOTNULL)BEGINDECLARE @retVal intEXEC @retVal = dbo.sp_syscollector_verify_collection_set@collection_set_id OUTPUTIF (@retVal <> 0)BEGINRETURN (1)ENDEND DECLARE @TranCounter INTSET @TranCounter =@@TRANCOUNTIF (@TranCounter > 0)SAVETRANSACTION tran_cleanup_collection_setELSEBEGINTRANSACTION BEGINTRY-- changing isolation level to repeatable to avoid any conflicts that may happen-- while running this stored procedure and sp_syscollector_start_collection_set concurrentlySETTRANSACTIONISOLATIONLEVELREPEATABLEREAD -- Security check (role membership)IF (NOT(ISNULL(IS_MEMBER(N'dc_admin'), 0)= 1)ANDNOT(ISNULL(IS_MEMBER(N'db_owner'), 0)= 1))BEGINREVERTRAISERROR(14677,-1,-1,'dc_admin')RETURN (1)END -- Disable constraints-- this is done to make sure that constraint logic does not interfere with cleanup processALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal NOCHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Delete data collector jobsDECLARE @job_id uniqueidentifierDECLARE datacollector_jobs_cursor CURSORLOCALFORSELECT collection_job_id AS job_id FROMsyscollector_collection_setsWHERE collection_job_id ISNOTNULLAND( collection_set_id = @collection_set_id OR @collection_set_id ISNULL)UNIONSELECT upload_job_id AS job_id FROMsyscollector_collection_setsWHERE upload_job_id ISNOTNULLAND( collection_set_id = @collection_set_id OR @collection_set_id ISNULL) OPEN datacollector_jobs_cursorFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idWHILE (@@fetch_status= 0)BEGINIFEXISTS(SELECTCOUNT(job_id)FROM sysjobs WHERE job_id = @job_id )BEGINDECLARE @job_name sysnameSELECT @job_name = name from sysjobs WHERE job_id = @job_idPRINT'Removing job '+ @job_nameEXEC dbo.sp_delete_job@job_id=@job_id, @delete_unused_schedule=0ENDFETCHNEXTFROM datacollector_jobs_cursor INTO @job_idENDCLOSE datacollector_jobs_cursorDEALLOCATE datacollector_jobs_cursor -- Enable Constraints backALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_collection_sysjobsALTERTABLE dbo.syscollector_collection_sets_internal CHECKCONSTRAINT FK_syscollector_collection_sets_upload_sysjobs -- Disable trigger on syscollector_collection_sets_internal-- this is done to make sure that trigger logic does not interfere with cleanup processEXEC('DISABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- Set collection sets as not running state and update collect and upload jobs as nullUPDATE syscollector_collection_sets_internalSET is_running = 0,collection_job_id =NULL,upload_job_id =NULLWHERE (collection_set_id = @collection_set_id OR @collection_set_id ISNULL) -- Enable back trigger on syscollector_collection_sets_internalEXEC('ENABLE TRIGGER syscollector_collection_set_is_running_update_trigger ON syscollector_collection_sets_internal') -- re-set collector config store if there is no enabled collectorDECLARE @counter INTSELECT @counter=COUNT(is_running)FROM syscollector_collection_sets_internal WHERE is_running = 1 IF (@counter = 0)BEGINUPDATE syscollector_config_store_internalSET parameter_value = 0WHERE parameter_name IN('CollectorEnabled'); UPDATE syscollector_config_store_internalSET parameter_value =NULLWHERE parameter_name IN('MDWDatabase','MDWInstance')END -- Delete collection set logsDELETEFROM syscollector_execution_log_internalWHERE (collection_set_id = @collection_set_id OR @collection_set_id ISNULL) IF (@TranCounter = 0)BEGINCOMMITTRANSACTIONENDRETURN(0)ENDTRYBEGINCATCHIF (@TranCounter = 0 ORXACT_STATE()=-1)ROLLBACKTRANSACTIONELSEIF (XACT_STATE()= 1)ROLLBACKTRANSACTION tran_cleanup_collection_set DECLARE @ErrorMessageNVARCHAR(4000);DECLARE @ErrorSeverityINT;DECLARE @ErrorStateINT;DECLARE @ErrorNumberINT;DECLARE @ErrorLineINT;DECLARE @ErrorProcedure NVARCHAR(200);SELECT @ErrorLine =ERROR_LINE(),@ErrorSeverity =ERROR_SEVERITY(),@ErrorState =ERROR_STATE(),@ErrorNumber =ERROR_NUMBER(),@ErrorMessage =ERROR_MESSAGE(),@ErrorProcedure =ISNULL(ERROR_PROCEDURE(),'-');RAISERROR (14684, @ErrorSeverity,-1 , @ErrorNumber, @ErrorSeverity, @ErrorState, @ErrorProcedure, @ErrorLine, @ErrorMessage);RETURN (1)ENDCATCHEND
相关文章推荐
- 删除Management Data Warehouse (MDW) job失败
- 删除Management Data Warehouse (MDW) job失败
- 对数据集“dsArea”执行查询失败。 (rsErrorExecutingCommand),Query execution failed for dataset 'dsArea'. (rsErrorExecutingCommand),Manually process the TFS data warehouse and analysis services cube
- 删除被锁定的Datastage Job
- 表扩展失败(ORA-01653)后的空间管理问题【THE SPACE MANAGEMENT PROBLEM OF THE TABLE EXTEND FAILD 】 推荐
- 场景4 Data Warehouse Management 数据仓库
- Exchange 2010,删除系统默认地址列表后, OWA 通讯簿失败问题解决办法 推荐
- Designing a Data Warehouse : Supporting Customer Relationship Management
- 用SqlDataAdapter.DeleteCommand根据DataSet删除数据库记录的失败教训
- 删除恢复Hadoop集群中的DataNode
- 删除 对于 数据库“Teacher”失败。
- Hadoop datanode添加与删除
- stsadm.exe工具删除创建失败的SSP
- (1)顺序表的操作 ① 输入一组整型元素序列,建立线性表的顺序存储结构。 ② 实现该线性表的遍历。 ③ 在该顺序表中查找某一元素,查找成功显示查找元素,否则显示查找失败。 ④ 在该顺序表中删除或插入指
- EMD_MAINTENANCE.EXECUTE_EM_DBMS_JOB_PROCS的删除创建
- DataStage Job Monitor problems
- 卸载MicrosoftBAF(删除C:\CommonFramework\instdata.dat)
- 成功或失败,绩效说了算 推荐
- 域控制器降级失败后如何删除 Active Directory 中的数据
- jquery easyui datagird删除最后一页,显示前一页