DELETE 語句與 REFERENCE 約束"FK_subplan_job_id"沖突,。該沖突發(fā)生于數(shù)據(jù)庫"msdb",,表"dbo.sysmaintplan_subplans", column 'job_id',。
語句已終止,。 (Microsoft SQL Server,,錯誤: 547)
有關(guān)幫助信息,,請單擊: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=09.00.1399&EvtSrc=MSSQLServer&EvtID=547&LinkId=20476
=============
下面是解決辦法,執(zhí)行下面代碼就可以了:
與維護(hù)計(jì)劃有關(guān)的有三張表:
1.sysmaintplan_log:在維護(hù)計(jì)劃運(yùn)行后,,會在此表中進(jìn)行記錄,;
2.sysjobschedules:代理作業(yè)的作業(yè)信息
3.sysmaintplan_subplans:記錄維護(hù)計(jì)劃的子計(jì)劃信息
4.sysjobs_view:相關(guān)的作業(yè)信息
這4張表有著PK和FK的關(guān)聯(lián)關(guān)系,在刪除作業(yè)時,,就容易因?yàn)榘l(fā)生FK的沖突而導(dǎo)致失敗,。
具體的處理方法如下:
USE [msdb]
declare @job_name varchar(100)
set @job_name = N'作業(yè)名稱'
--刪除在計(jì)劃里面的日志
DELETE sysmaintplan_log
FROM sysmaintplan_subplans AS subplans INNER JOIN
sysjobs_view AS syjobs ON subplans.job_id = syjobs.job_id INNER JOIN
sysmaintplan_log ON subplans.subplan_id = sysmaintplan_log.subplan_id
WHERE (syjobs.name = @job_name)
--刪除代理的作業(yè)
DELETE sysjobschedules
FROM sysjobs_view v INNER JOIN sysjobschedules o ON v.job_id=o.job_id WHERE v.name=@job_name
--刪除子計(jì)劃
DELETE sysmaintplan_subplans
FROM sysmaintplan_subplans AS subplans INNER JOIN
sysjobs_view AS syjobs ON subplans.job_id = syjobs.job_id
WHERE (syjobs.name = @job_name)
--刪除作業(yè)
DELETE FROM msdb.dbo.sysjobs_view WHERE NAME = @job_name