If(OBJECT_ID('tempdb..#temp') Is Not Null)
begin
    drop table #temp;
end
begin
	select JobID into #temp
from VCLib.T_JOB j
join VCLib.T_JOB_STEP js
on j.JobID = js.Job
join VCLib.T_SCHEDULER_OPERATION so
on so.OperationID = js.Operation
where so.ProcessName = 'CheckAllDataformTemplatesProcess'
end

if exists(select * from #temp)
begin

	delete from 
		VCLib.T_JOB_SCHEDULE
	where Job in (select * from #temp);

	delete er from 
		VCLib.T_JOB_STEP js
	left join VCLib.T_JobExecutionError er
	on js.JobStepID = er.Step
	where js.Job in (select * from #temp);

	delete js from 
		VCLib.T_JOB_STEP js
	left join VCLib.T_JobExecutionError er
	on js.JobStepID = er.Step
	where js.Job in (select * from #temp);
	
	delete from
		VCLib.T_JOB
	where JobID in (select * from #temp);
end

delete from 
VCLib.T_SCHEDULER_OPERATION
where ProcessName = 'CheckAllDataformTemplates'
