03.25.08

Mass update sql job owners

Posted in DBA, Microsoft Sql server, TSQL at 12:20 pm by

After a domain migration you might find yourself in a position where you have a whole bunch of jobs with dead domain accounts. Then you could use sql server management studio to update the job ownership one by one. Or you could make a script which updates it for you. The next script gives you a the basics to script this. Just change the OLD_ACCOUNT by the account you want to replace and the NEW_ACCOUNT by the account you want to replace it with.
SELECT ‘EXEC MSDB.dbo.sp_update_job ‘ + char(13) +
‘@job_name = ‘ + char(39) + j.[Name] + char(39) + ‘,’ + char(13) +
‘@owner_login_name = ‘ + char(39) + ‘NEW_ACCOUNT’ + char(39) + char(13) + char(13)
FROM MSDB.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
WHERE l.[name] = ‘OLD_ACCOUNT’
ORDER BY j.[name]

The output of this script should copy paste and run in the sql server management console.

Comments are closed.