{"id":80,"date":"2008-12-24T09:52:00","date_gmt":"2008-12-24T15:52:00","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=80"},"modified":"2015-12-28T16:35:55","modified_gmt":"2015-12-28T22:35:55","slug":"t-sql-change-sql-job-owners-to-sa","status":"publish","type":"post","link":"https:\/\/practical-prints.com\/?p=80","title":{"rendered":"T-SQL &#8211; Change SQL Job Owners to SA"},"content":{"rendered":"<p>I&#8217;m a sucker for the Maint Wizards and everytime I touch a SQL Job.. It reverts the ownership to my domain account. ( Not a Good Practice to use my domain account &#8230; I know). I manually change the ownership back to sa (actually, dbo as sa is disabled&#8230;ssssh) . OR I CAN USE THE BELOW HANDY-DANDY SCRIPT . As always .. Buyer-Beware! Written\u00a0for \u00a0SQL Server 2005..<\/p>\n<p>Here is a script I wrote to do the heavy lifting:<\/p>\n<p><!--more--><\/p>\n<p>USE [msdb]<br \/>\nGO<br \/>\n&#8212; Surpress the rows affected message<br \/>\nSET NOCOUNT ON<br \/>\nGO<br \/>\n\/*<br \/>\nScript to Change SQL JOB owners to sa.<br \/>\nSome jobs may want to have alternate<br \/>\nowner for example a domain login or<br \/>\nlocal server login.<\/p>\n<p>By Default, whenever a sysadmin changes a job<br \/>\non the server the ownership changes to<br \/>\nthat admin.<\/p>\n<p>The following script changes those jobs to<br \/>\nsa ownership. It&#8217;s easy to convert this to a<br \/>\nstored procedure.<\/p>\n<p>&#8212; JCD 12-24-2008<\/p>\n<p>MERRY CHRISTMAS!!!\u00a0 or<br \/>\nEnjoy Whatever Holiday Your Faith Recognizes.<\/p>\n<p>I&#8217;m a Christian so CHRISTMAS ROCKS 4 ME!<\/p>\n<p>*\/<br \/>\n&#8212; Create Variables to use to setp through temp table<br \/>\nDECLARE @i INT<br \/>\nSET @i = 1<\/p>\n<p>&#8212; Check for an Drop Temp table #SYSJOB_IDS<br \/>\nIF OBJECT_ID(&#8216;tempdb..#SYSJOB_IDS&#8217;,&#8217;u&#8217;)IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP TABLE #SYSJOB_IDS<br \/>\nEND<br \/>\nBEGIN<br \/>\nCREATE TABLE #SYSJOB_IDS (<br \/>\n[id] INT IDENTITY(1,1) NOT NULL<br \/>\n,[job_ID] UNIQUEIDENTIFIER NOT NULL<br \/>\n,[name] SYSNAME NOT NULL<br \/>\n,[CurrOwner] SYSNAME NOT NULL<br \/>\n)<br \/>\nEND<\/p>\n<p>\/*<br \/>\nPopulate Temp Table With Job_ID&#8217;s that<br \/>\ndo not have an owner of sa.<br \/>\n*\/<\/p>\n<p>INSERT INTO #SYSJOB_IDS(job_ID, [name],[CurrOwner])<br \/>\nSELECT DISTINCT<br \/>\na.[job_id]<br \/>\n,a.[name]<br \/>\n,b.[name][CurrOwner]<br \/>\nFROM msdb..SYSJOBS as a<br \/>\nJOIN master.sys.syslogins as b<br \/>\nON b.[sid] = a.[owner_sid]<br \/>\nWHERE b.[name] &lt;&gt; &#8216;sa&#8217;<br \/>\nORDER BY a.[name]<\/p>\n<p>\/*<br \/>\n&#8211; Loop Thru of Temp Table #SYSJOB_IDS<br \/>\nto populate @job_id<br \/>\n&#8211; EXEC msdb.dbo.sp_update_job command<br \/>\nto change JOB OWNER (OWNER_SID) to &#8216;SA&#8217;<br \/>\n*\/<br \/>\nWHILE @i &lt; = (SELECT MAX(id) from #SYSJOB_IDS)<br \/>\nBEGIN<br \/>\nDECLARE @job_id NVARCHAR(36)&#8211; Used to pass job_id<br \/>\n,@name sysname<br \/>\n,@CurrOwner NVARCHAR(40)<br \/>\n&#8212; Populate From temp Table<br \/>\nSELECT TOP 1<br \/>\n@job_id = [job_ID]<br \/>\n,@name = RTRIM([name])<br \/>\n,@CurrOwner = RTRIM([CurrOwner])<br \/>\nFROM #SYSJOB_IDS<br \/>\nWHERE id = @i<\/p>\n<p>Declare @Run INT<\/p>\n<p>EXECUTE @Run = [msdb].[dbo].[sp_update_job] @job_id=@job_id , @owner_login_name=N&#8217;sa&#8217;<br \/>\n&#8211;Litte Error Checking and Display Message upon execution<br \/>\nIF @Run = 0<br \/>\nBEGIN<br \/>\n&#8212; Display Message that Job Has been updated<br \/>\nSELECT UPPER(@name) + &#8216; owner was changed from &#8216;+ UPPER(@CurrOwner)+&#8217; to sa.&#8217;<br \/>\nEND<br \/>\nELSE IF @Run &lt;&gt; 0<br \/>\nBEGIN<br \/>\n&#8212; Display Message that Job Has been updated<br \/>\nSELECT &#8216;Fail: &#8216;+ UPPER(@name) + &#8216; owner was not changed from &#8216;+ UPPER(@CurrOwner)+&#8217;.&#8217;<br \/>\nEND<\/p>\n<p>&#8211;Increment @i to go to Next Value<br \/>\nSELECT @i = @i+1<br \/>\nEND<\/p>\n<p>GO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a sucker for the Maint Wizards and everytime I touch a SQL Job.. It reverts the ownership to my domain account. ( Not a Good Practice to use my domain account &#8230; I know). I manually change the ownership &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/practical-prints.com\/?p=80\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_wpcom_ai_launchpad_first_post":false,"footnotes":""},"categories":[273,266,77,22],"tags":[218,346,249,347,52,344,345,291,343],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-administration","category-dba","category-fromdigg","category-sql-server","tag-cursor","tag-msdb","tag-script","tag-sp_update_job","tag-ssms","tag-sysjobs","tag-sysname","tag-t-sql","tag-tempdb"],"jetpack_shortlink":"https:\/\/wp.me\/phfTTO-1i","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/practical-prints.com\/index.php?rest_route=\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practical-prints.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practical-prints.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practical-prints.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/practical-prints.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":7,"href":"https:\/\/practical-prints.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/practical-prints.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions\/521"}],"wp:attachment":[{"href":"https:\/\/practical-prints.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practical-prints.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practical-prints.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}