Skip to content

Instantly share code, notes, and snippets.

@petesql
Created February 2, 2024 16:31
Show Gist options
  • Select an option

  • Save petesql/32f321c3540a593d5b1f0d3c2bf8c456 to your computer and use it in GitHub Desktop.

Select an option

Save petesql/32f321c3540a593d5b1f0d3c2bf8c456 to your computer and use it in GitHub Desktop.
Disable All SQL Agent Jobs
-- Generate SQL script to disable enabled SQL Server Agent jobs
DECLARE @job_id UNIQUEIDENTIFIER
DECLARE @sqlScript NVARCHAR(MAX) = ''
DECLARE job_cursor CURSOR FOR
SELECT job_id
FROM msdb.dbo.sysjobs
WHERE enabled = 1; -- Only select jobs that are currently enabled
OPEN job_cursor
FETCH NEXT FROM job_cursor INTO @job_id
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlScript += 'EXEC msdb.dbo.sp_update_job @job_id = ''' + CONVERT(NVARCHAR(36), @job_id) + ''', @enabled = 0;' + CHAR(13) + CHAR(10)
FETCH NEXT FROM job_cursor INTO @job_id
END
CLOSE job_cursor
DEALLOCATE job_cursor
-- Print the generated SQL script
PRINT @sqlScript
@petesql

petesql commented Feb 15, 2025

Copy link
Copy Markdown
Author

Checkout my blog post that shows an example using this script:
Generate SQL commands to enable or disable all SQL Server Agent jobs
https://peter-whyte.com/2025/02/sql-server-script-enable-or-disable-all-sql-agent-jobs/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment