سؤال

I have a stored procedure that gets a job name as input and send mail with job history information. How can I automatically runs this job when other jobs fail?

Is there a way to pass the name (or ID) of the job to this stored procedure?

Thanks

هل كانت مفيدة؟

المحلول

You can use SQL Agent tokens to get the name or ID of the currently executing job (http://technet.microsoft.com/en-us/library/ms175575(v=sql.105).aspx).

To set this up, do the following:

  1. Add new step "Email job history" to the end of your job
  2. Change each job step to have an "OnFailure" action of "Go to step: {n} Email job history", where {n} is the step # of the step added above.
  3. Use the SQL Agent tokens to get your currently executing JOBID and any other info you need. Example of "Email job history" T-SQL step below, where msdb.dbo.usp_SQLAgentSendJobStepFailedEmail would be your procedure name:

Example of "Email job history" T-SQL step:

DECLARE @jobid uniqueidentifier, @run_date int, @run_time int, @profile_name sysname;

set @profile_name = 'MyDatabaseMailProfileName';

--USE SQL AGENT "TOKENS" TO GET CURRENT JOB PARAM VALUES
set @jobid = $(ESCAPE_NONE(JOBID));
set @run_date = $(ESCAPE_NONE(STRTDT));
set @run_time = $(ESCAPE_NONE(STRTTM));

EXEC msdb.dbo.usp_SQLAgentSendJobStepFailedEmail @jobid = @jobid, @run_date = @run_date, @run_time = @run_time, @email_profile_name = @profile_name;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top