EnVisageOnline/Main/Database/Scripts/20150924/3_AddJobToAgent.sql

55 lines
1.6 KiB
Transact-SQL

--@job and @databasename form the name of the job in sql agent.
DECLARE @job nvarchar(128)='db maintenance'
DECLARE @mycommand nvarchar(max) ='EXEC [sp_DBMaintenance]'
DECLARE @servername nvarchar(28) ='ALB-335034L'
DECLARE @startdate nvarchar(8) ='20150924'
DECLARE @starttime nvarchar(8)='140000'
declare @databasename nvarchar(20)='EnVisage'
declare @schName nvarchar(20)='Monthly Schedule'
declare @stepName nvarchar(20)='process step'
/*
1=once
4=daily
8=weekly
16=monthly
*/
declare @freqtype int =16 -- run monthly
declare @freqinterval int=16 --On the freq_interval day of the month.
declare @freqrecurrencefactor int =1 -- how many occurances to skip between exec
--end parms
set @job=@job+'_'+@databasename
--delete any entries if we have one for this job
begin try
exec dbo.sp_delete_job @job_name=@job
end try
BEGIN CATCH
--do nothing. we failed to remove the job. it might not exist..
END CATCH
--Add a job
EXEC dbo.sp_add_job
@job_name = @job ;
--Add a job step named process step. This step runs the stored procedure
EXEC sp_add_jobstep
@job_name = @job,
@step_name = @stepName,
@subsystem = N'TSQL',
@command = @mycommand,
@database_name=@databasename;
--Schedule the job at a specified date and time
exec sp_add_jobschedule @job_name = @job,
@name = @schName,
@freq_type=@freqtype,
@freq_interval=@freqinterval,
@active_start_date = @startdate,
@active_start_time = @starttime,
@freq_recurrence_factor =@freqrecurrencefactor
-- Add the job to the SQL Server Server
EXEC dbo.sp_add_jobserver
@job_name = @job,
@server_name = @servername