I am trying to store all the SQL Server Agent Jobs in Table name and want to execute them based on their loading frequency.
CREATE TABLE Maintainance
(
SQLJobNa
a) Create a proc that runs through list of procs with appropriate frequency
value, executes them and updates lastrun
column.
@frequency
in it, pass it form outside.e.g.
alter proc dbo.RunProcs
@Frequency varchar(50)
as
begin
declare @crProcs cursor
set @crProcs = cursor fast_forward for
select m.ProcName
from dbo.Maintainance m
where m.Frequency = @Frequency
order by 1
...
while @@fetch_status = 0
beign
begin try
exec @ProcName
...
update -> succeded
...
end try
begin catch
...
update -> failed
...
end catch
fetch ...
end
return 1
end
b) Create scheduled jobs to run this proc
Create a job "DAILY PROCS", assign a schedule to this job to make it run every day.
Add a job-step with T-SQL:
exec dbo.RunProcs @Frequency = 'day'
Create a job "WEEKLY PROCS", assign a schedule to this job to make it run every week
Add a job-step with T-SQL:
exec dbo.RunProcs @Frequency = 'week'
Create a job "MONTHLY PROCS", assign a schedule to this job to make it run every month
Add a job-step with T-SQL:
exec dbo.RunProcs @Frequency = 'month'