SQL Server Agent jobs automate tasks within SQL Server, but there are times when you need to enable or disable multiple jobs, such as during maintenance, troubleshooting, or system upgrades. This guide provides a script that generates SQL commands to enable or disable all SQL Server Agent jobs by changing a flag in the script.
Enable or Disable SQL Server Agent Jobs with a Single Script
The script below iterates through all SQL Agent jobs and generates commands to either enable or disable them. The @enabled
flag determines whether jobs are enabled (1
) or disabled (0
).

Execution Steps:
1. Set @enabledFlag
to 1
to enable jobs or 0
to disable them.
2. Run the script in SQL Server Management Studio (SSMS).
3. Copy and save the output (you want to keep a copy of what you’re disabling).
4. Execute the generated commands to apply the changes.

In the example above, we have the refreshed SQL Server Agent job list in the SSMS Object Explorer which shows all jobs are disabled.
Why This Script is Useful
This script to generate SQL for disabling/enabling Agent Jobs is particularly useful in the following cases:
Always On Availability Group Failover
In environments with Always On Availability Groups (AG), each SQL Server instance may have its own set of SQL Server Agent jobs. When failing over from the primary to the secondary replica, it’s essential to disable jobs on the secondary to ensure that only the active server executes them. This prevents duplicate job executions and maintains smooth operations during failover events.
Server Builds, Migrations, and Maintenance
During server builds, migrations, or maintenance activities, it’s often necessary to temporarily disable all SQL Agent jobs to avoid execution conflicts. This ensures that critical tasks, such as database maintenance or testing, proceed without interference. Always remember to back up job configurations before making any changes, especially in environments with a large number of jobs.
Patch or Upgrade Processes
When performing patches or upgrades, it’s crucial to prevent automated job execution that might interfere with the process. This script helps maintain control over job execution, minimizing the risk of issues during system updates.
Testing and Development
For test environments, this script provides a quick way to disable SQL Agent jobs, ensuring that no maintenance or background tasks disrupt your testing or development procedures.
By customizing this script, amending the @enabledFlag
from 0 to 1, we can use it to enable jobs instead of enabling them. We can also customize further to filter out any key jobs you don’t want to change – this would require an additional condition at line 9 in the script, using the columns in the dbo.sysjobs
system table.
We can also use a SQL Script to list all SQL Agent jobs in SQL Server for a more detailed review of all jobs including their schedules.
Remember to backup the list of jobs you are disabling, by copying the generated SQL to a file. I hope this post was useful for you and what you needed!
Leave a Reply