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.
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.
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.
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.
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With