生成 JOB 并将其分配给对象(例如存储过程)的典型脚本如下...
DECLARE @JobID BINARY(16)
EXECUTE msdb.dbo.sp_add_job @job_id = @JobID OUTPUT,
@job_name = N'<<A NAME OF YOUR JOB>>',
@enabled = 1,
@owner_login_name = N'@YOURLOGINCREDENTIALS',
@description = N'No description available.',
@category_name = N'[Uncategorized (Local)]',
@notify_level_eventlog = 0,
@notify_level_email = 2,
@notify_email_operator_name = N'<<OPTIONAL>>',
@notify_level_netsend = 0,
@notify_level_page = 0,
@delete_level = 0;
EXECUTE msdb.dbo.sp_add_jobstep @job_id = @JobID,
@step_id = 1,
@step_name = N'spMYPROCEDURE',
@command = N'EXEC spMYPROCEDURE',
@database_name = N'<<MASTER OR WHAT EVER YOUR DATABASE IS>>',
@subsystem = N'TSQL',
@flags = 6,
@retry_attempts = 3,
@retry_interval = 5,
@output_file_name = N'<<WHERE YOU WANT TO SAVE A LOG FILE OF THE JOB>> \spMYPROCEDURE.jobresults.log',
@on_success_step_id = 0,
@on_success_action = 1,
@on_fail_step_id = 0,
@on_fail_action = 2;
EXECUTE msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1;
EXECUTE msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(LOCAL)';
EXECUTE msdb.dbo.sp_add_jobschedule @job_id = @JobID,
@name = N'spMYPROCEDURE',
@enabled = 1,
@freq_type = 8,
@active_start_date = 20140417,
@active_end_date = 99991231,
@freq_interval = 62,
@freq_subday_type = 1,
@freq_subday_interval = 1,
@freq_relative_interval = 1,
@freq_recurrence_factor = 1,
@active_start_time = 90000,
@active_end_time = 235959;
GO
通常我会先创建存储过程,测试并运行它以确保它可以正常工作,然后为它分配一个作业,如上所示。
请注意@active 时间都是完整的整数小数(即:90000 = 09:00:00am)
USE [master]
在运行上述脚本之前,您可能还需要作为数据库参考。