Disabling A SQL Agent job from within the Job

Is it possible to disable a SQL Agent job using the tsql commands from a job step within the job itself? The situation is that we have a job that runs hourly to move data from a proprietary application to our SQL Database. We've had some instances where unexpected invalid data came across from the proprietary application that caused step 3 of our 7 step job to fail after hours. Because this runs 24 hours, that same data was sent across every hour causing the same error every hour which made a mess of some of our data. We are working to tighten up our process to handle these thing more gracefully but as a stop-gap sort of fix, if, when one of the steps failed, we could disable the job so it wouldn't kick off again before someone had a chance to review the issue could save lots of cleanup time.
more ▼

asked Oct 27, 2011 at 10:00 AM in Default

stanglady1992 gravatar image

11 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort oldest

I would recommend looking at the sp_update_job system stored procedure. http://msdn.microsoft.com/en-us/library/ms188745.aspx

In your case, you would want to have your scheduled task set up with an error step that runs something similar to the following:

sp_update_job @job_name = '<my job name>', 
              @enabled = 0
Don't forget to set the final "real" job step to not move onto the error step unless it errors!
more ▼

answered Oct 27, 2011 at 10:09 AM

ThomasRushton gravatar image

ThomasRushton ♦
34.2k 18 20 44

(comments are locked)
10|1200 characters needed characters left

You sure can, just add a SQL step for on failure that runs

USE [msdb]


EXEC msdb.dbo.sp_update_job @job_name=N'', @enabled=0
more ▼

answered Oct 27, 2011 at 10:10 AM

SirSQL gravatar image

4.8k 1 3

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Oct 27, 2011 at 10:00 AM

Seen: 1700 times

Last Updated: Oct 27, 2011 at 10:00 AM