Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The proper way to disable/enable SQLServer Agent Jobs

I have a number of SQLServer agent scheduled jobs, one of them performs a full database backup. I want to disable some other jobs when backup begins and re-enable them once backup is done. What is the right way to do so? I was thinking about adding one of the following tsql commands to the first step of the backup task (and respective enable commands to the last step), but I cannot find which one is better (or maybe there is another way).

UPDATE MSDB.dbo.sysjobs
SET Enabled = 0
WHERE [Name] IN (....)

Or a number of EXEC dbo.sp_update_job ?
Thanks.

like image 955
a1ex07 Avatar asked Mar 23 '11 15:03

a1ex07


People also ask

How do I disable all SQL Server Agent jobs?

Go to the Job Activity Monitor and ctrl-left click on each, then right click and select disable job, or you can write a script against msdb. dbo. sysjobs and update the enabled column.

What does disabling a SQL job do?

Enable/Disable SQL job As mentioned, a disabled job will not be deleted; it will remain in an inactive state and will not be executed at the scheduled time. If the job is re-enabled, it will continue to get executed at the scheduled time and interval.

How do I find out who disabled a SQL Agent job?

First, you can check your TRC files in Program files/Microsoft Sql Server/Instance_Name/MSSQL/Logs in order to find out who logged into sql server. Also check Windows Event Viewer, in the Security Option.

How do I let someone execute a SQL Agent job they don't own?

There are only two ways that someone can have permission to execute a SQL Agent job. You must either own the job, or be a member of the role SQLAgentOperatorRole (found in msdb). Unfortunately SQLAgentOperatorRole grants permissions to run any job (among other things).


3 Answers

Definitely use sp_update_job. If the job is already scheduled, then manipulating the sysjobs table directly won't necessarily cause the cached schedule to be re-calculated.

It might work for the ENABLED flag (haven't tried it), but I know for a fact that it doesn't work for columns like start_step_id.

like image 61
BradC Avatar answered Oct 20 '22 18:10

BradC


You'd have to run EXEC dbo.sp_update_job because you can't update system tables directly (although I'm not sure if sysjobs still counts as a system table Mitch says it can be updated)

I would consider the use of sp_getapplock and sp_releaseapplock to "lock" other jobs out without actually updating the jobs though.

like image 38
gbn Avatar answered Oct 20 '22 18:10

gbn


I would use sp_update_job as it encapsulates reusable piece of logic that is supported. Why re-invent the wheel.

http://msdn.microsoft.com/en-us/library/ms188745.aspx

like image 39
Kuberchaun Avatar answered Oct 20 '22 19:10

Kuberchaun