Skip to content

Instantly share code, notes, and snippets.

@petesql
Created February 2, 2024 16:31
Show Gist options
  • Save petesql/32f321c3540a593d5b1f0d3c2bf8c456 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment