![]() |
|
|
|
By: Jeremy Kadlec | Read Comments (10) | Related Tips: More > SQL Server Agent |
Problem
As a portion of our backup and recovery process I need to disabled (or enable) all of our SQL Server Agent Jobs depending on the situation and the SQL Server instance. I know I can use the GUI to click through and make the changes, but on a number of our SQL Server instances we have 50+ jobs, so this is a monotonous task. Do you have any scripts to enable or disable all SQL Server Jobs? What about enabling or disabling all SQL Server Jobs by Job name or Job Category name?
Solution
The SQL Server Agent system tables can be updated without setting any sp_configure parameters, or an equivalent command, as is the case with the Master database. So building a script to meet your needs should be simple and straightforward. Before we dive into some scripts to address your need, another option to consider is just stopping or starting the SQL Server Agent service. Since the SQL Server Agent service is responsible for permitting the Jobs to run in the first place, then stopping and starting this service would also prevent and then enable the Jobs to run. The one short coming to that approach may be that you might not always have rights to manage the service or that only specific types of jobs need to be enabled or disabled. As those conditions arise, using the scripts below may be the best approach.
From a coding perspective, the following options are available to manage SQL Server Agent Jobs with a bit more flexibility:
|
Disable All SQL Server Agent Jobs |
| USE MSDB; GO UPDATE MSDB.dbo.sysjobs SET Enabled = 0 WHERE Enabled = 1; GO |
|
Enable All SQL Server Agent Jobs |
| USE MSDB; GO UPDATE MSDB.dbo.sysjobs SET Enabled = 1 WHERE Enabled = 0; GO |
|
Disable Jobs By Job Name |
| USE MSDB; GO UPDATE MSDB.dbo.sysjobs SET Enabled = 0 WHERE [Name] LIKE 'Admin%'; GO |
|
Disable Jobs By Job Category |
| USE MSDB; GO UPDATE J SET J.Enabled = 0 FROM MSDB.dbo.sysjobs J INNER JOIN MSDB.dbo.syscategories C ON J.category_id = C.category_id WHERE C.[Name] = 'Database Maintenance'; GO |
Next Steps
| Friday, January 18, 2008 - 3:06:44 PM - admin | Read The Tip |
|
To the MSSQLTips.com community, As an update to this tip, our team has observed some issues with SQL Server 2000 SP4 with respect to determining the 'Next Run Date' without restarting the SQL Server Agent Service. Please note this potential issue and test accordingly. Thank you, |
|
| Tuesday, March 03, 2009 - 12:58:24 PM - sgr | Read The Tip |
|
I tried using this method (changing sysjobs.enabled from 1 to 0) for disabling a group of jobs which contained specific text using the like predicate (SQL 2K5 SP4 Enterprise x64). However, the jobs still ran. In SSMS under Jobs they show disabled (little red arrow on job), on job activity monitor they show disabled and on job properties they show disabled. The schedule is still enabled but I tested this idea by right clicking on each job and selecting disable [one at a time...ugh] and it only seems to disable the job not the schedule. Is the only way to disable a job using TSQL by using sp_update_job? I don't like this concept because I cannot disable a group of jobs at one a time easily. Thank you! |
|
| Tuesday, November 10, 2009 - 5:49:44 PM - pshashi | Read The Tip |
| hi, I have the exact same problem that SGR is describing. I have tested the below script many times. The jobs are showing as they were disabled in SQLAgent, after executing this script BUT the backups are running as usal. USE MSDB; GO UPDATE MSDB.dbo.sysjobs SET Enabled = 0 WHERE [Name] LIKE 'LSBackup%'; GO Is there anything I'm missing?? hi SGR, Jeremy, did you resolve the issue? thanks | |
| Tuesday, February 02, 2010 - 1:26:15 PM - littlefuzz | Read The Tip |
| The reason a direct update of the Enabled field doesn't work is that SQL Agent has cached this information and won't see the change until it is restarted. (Taken from here: http://www.sqlservercentral.com/Forums/Topic816271-146-1.aspx) | |
| Thursday, March 24, 2011 - 11:49:40 AM - lmaire | Read The Tip |
|
I have tried to use the mentionned method to enable/disable a job using T-SQL, and I was facing the same behaviour as sgr mentionned here before (under SQL2000 server): although the status was correctly updated in hte job list, the job was actually acting keeping its previous status (set manually). I found the solution (at least for me it's working) :
you should rather use the stored procedure "sp_update_job" :
Hope it helps... Laurent. |
|
| Friday, September 09, 2011 - 5:00:28 AM - Tarni | Read The Tip |
|
hi, i have faced the same problem and found that updating the system table sysjobs doesnt work.it the jobs run. even though i disabled the corresponding shedule from sysshedule the job still runs. then i restarted the Agent. and then job was actually disabled.
but this is not the case with sp_update_job it disables the job without restarting the agent.
Thanks |
|
| Wednesday, June 06, 2012 - 8:37:23 PM - Muhammad Sharjeel Ahsan | Read The Tip |
|
Great Job |
|
| Thursday, January 03, 2013 - 5:50:46 AM - hanumanthareddy | Read The Tip |
|
Sql jobs are not working under sql server agent in sql server 2008
I got below error like these Job_Name=Km_Report
Date 1/2/2013 11:45:00 PM Log Job History (KM_Report)
Step ID 0 Server CATOM-GMAPDVW69 Job Name KM_Report Step Name (Job outcome) Duration 00:00:39 Sql Severity 0 Sql Message ID 0 Operator Emailed Operator Net sent Operator Paged Retries Attempted 0
Message The job failed. The Job was invoked by Schedule 13 (KM_Report). The last step to run was step 1 (KM_Report).
2nd
Date 1/2/2013 11:45:01 PM Log Job History (KM_Report)
Step ID 1 Server CATOM-GMAPDVW69 Job Name KM_Report Step Name KM_Report Duration 00:00:38 Sql Severity 0 Sql Message ID 0 Operator Emailed Operator Net sent Operator Paged Retries Attempted 0
Message Executed as user: CATOM-GMAPDVW69\SYSTEM. 016 Source: Description: Failed to decrypt protected XML node "SQLPassword" with error 0x8009000B "Key not valid for use in specified state.". You may not be authorized to access this information. This error occurs when there is a cryptographic error. Verify that the correct key is available. End Error DTExec: The package execution returned DTSER_FAILURE (1). Started: 11:45:01 PM Finished: 11:45:39 PM Elapsed: 38.25 seconds. The package execution failed. The step failed.
|
|
| Monday, January 07, 2013 - 7:37:31 AM - Jeremy Kadlec | Read The Tip |
|
hanumanthareddy, I think this is the portion of the error message you need to focus on from your DTEXEC command: Failed to decrypt protected XML node "SQLPassword" with error 0x8009000B "Key not valid for use in specified state.". You may not be authorized to access this information. This error occurs when there is a cryptographic error. Verify that the correct key is available. HTH. Thank you, |
|
| Monday, February 18, 2013 - 11:12:19 PM - adam | Read The Tip |
|
I'm happier using sp_update_job, rather than updating sysjobs directly, mainly to get around the caching issue. Just stick the query logic from above in a cursor first, eg, for "Disable Jobs By Job Category": DECLARE @SelectedJobId uniqueidentifier |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |