Change Job owner for all SQL Server Agent Jobs - SQL Server Scripts

I was checking my emails and saw below error message from SQL Server monitoring tool.
"The job failed.  The owner '' of job '' does not have server access.",

That means a DBA had created a SQL Server Agent  job with his name as owner. Once he/she will leave the company and account will be removed from AD, the jobs will start failing.

It is good idea to use service account or sa as SQL Server Job owner, so you don't have to worry in case owner leave the company:)


Below Query will return you Job names and their owners.

SELECT J.NAME AS JobName
    ,L.NAME AS OwnerName
FROM msdb..sysjobs J
LEFT JOIN master.sys.syslogins L ON J.owner_sid = L.sid


Now if we would like to update all the job where owner is not sa, we can use below query. You can also modify the query to filter the jobs for which you would like to update owner.

--Provide the New Owner for The Jobs you like to Change, I am using sa
DECLARE @NewOWNERName VARCHAR(100)

SET @NewOWNERName = 'sa'

DECLARE @OldJobOwner VARCHAR(100)
DECLARE @JobName VARCHAR(1000)

DECLARE Job_Cursor CURSOR
FOR
--Change your Query as per requirements, I am selecting all Job where owner<>sa
SELECT J.NAME AS JobName
    ,L.NAME AS OwnerName
FROM msdb..sysjobs J
LEFT JOIN master.sys.syslogins L ON J.owner_sid = L.sid
WHERE L.NAME <> @NewOWNERName

OPEN Job_Cursor

FETCH NEXT
FROM Job_Cursor
INTO @JobName
    ,@OldJobOwner

WHILE (@@FETCH_STATUS <> - 1)
BEGIN
    EXEC msdb..sp_update_job @job_name = @JobName
        ,@owner_login_name = @NewOWNERName

    PRINT 'Ownerd Change for ' + @JobName + 'Job from ' + @OldJobOwner + ' to '
 + @NewOWNERName

    FETCH NEXT
    FROM Job_Cursor
    INTO @JobName
        ,@OldJobOwner
END

CLOSE Job_Cursor

DEALLOCATE Job_Cursor


Just to be on safe side, test the script in Development environment before run in production. 

14 comments:

  1. There is something liberating and thrilling about knowing that any casino game that you could possibly want to play is available at your fingertips.

    sbobet88 mobile

    ReplyDelete
  2. The company web site is rapidly becoming the first point of contact for most job seekers or prospective clients. Contractors should update their corporate web site so that it provides a professional and interactive presentation of the firm, its goals, key personnel, corporate culture, top achievements, and business philosophy.Cover letter

    ReplyDelete
  3. Establish a Timeline: Your goals establish what you want to do with your career and how you can develop it through incremental steps. Your priorities determine the immediacy of your goals. For example, ashamed of being unemployed a goal and top priority may be finding a job immediately.

    ReplyDelete

  4. This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this..
    Jobs

    ReplyDelete
  5. I adore your websites way of raising the awareness on your readers.fake packwoods

    ReplyDelete

  6. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
    먹튀사이트검증

    ReplyDelete
  7. It is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it! For more visit here construction employment recruiters in USA

    ReplyDelete
  8. I’ve been absent for a while, but now I remember why I used to love this website. Thank you, I will try and check back more often. How frequently you update your site? Pharmacist Jobs in Government

    ReplyDelete
  9. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! romani in uk

    ReplyDelete
  10. If we had to relate this to a job description, then it would probably entail the production of goods in a manner that is more efficient than it would have been produced under traditional means. If this is the case then I propose this basic definition: California environmental jobs

    ReplyDelete
  11. The fifth factor that can produce peak performance in sports is actually finding the right players to fill up the most key positions of the team first. In any sport teams, there will be key positions that must be filled first. portable dartboard stand

    ReplyDelete
  12. Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. ランディングページ制作

    ReplyDelete
  13. It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested one.I concur with your conclusions and will eagerly look forward to your future updates. need job fast

    ReplyDelete
  14. No doubt Social Media Networking - Facebook, Twitter, LinkedIn and sources like DIGG are powerful agents, however one must realize that they are informational/promotional platforms and vehicles - in themselves, they are not a Marketing solution. Salesforce interview questions and answers

    ReplyDelete