Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 16:17
Show Gist options
  • Save Otterpohl/a75ffbf2e33aa8f75d1364e4e3a8dd77 to your computer and use it in GitHub Desktop.
Save Otterpohl/a75ffbf2e33aa8f75d1364e4e3a8dd77 to your computer and use it in GitHub Desktop.
Sets all agent jobs owner to sa account
SELECT 'EXEC msdb.dbo.sp_update_job @job_id=N''' + CAST(j.job_id AS VARCHAR(150)) + ''', @owner_login_name=N''sa'' '
FROM msdb.dbo.sysjobs AS j
INNER JOIN master.sys.syslogins AS L
ON j.owner_sid = L.sid
INNER JOIN msdb.dbo.syscategories AS C
ON C.category_id = j.category_id
WHERE msdb.dbo.SQLAGENT_SUSER_SNAME(j.owner_sid) <> 'sa';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment