简体   繁体   中英

Runas bulk update in SQL Server 2005

I have about 300+ jobs in SQL Server 2005, and most of those jobs have a "Runas" under different user other than me. Please help with a query where I could do a bulk update to change the "Runas" from the old user to me? I don't want to go through each job and double clicking on each of the stepid and change the "Runas" user, please help, thanks.

TL

I am unable to test this in SQL Server 2005 (the below works in 2008 and 2012). According to technet , the same syntax is applicable to 2005.

As you may not want to update jobs that are related to maintenance tasks, I feel the below steps are the best way to do this. Please note that this will only change steps that do not require a system account to run, ie, it will not update PowerShell steps and similar.

Make sure you run all the below in your MSDB database as that is where all your jobs are held in SQL Server. Run the following select statement (replace the UserName with the user you want to use, if using a domain/network name prefix with domain\\username):

SELECT 'EXEC sp_update_jobstep @job_name = ''' + sj.name + ''', @step_id = ' + 
Convert(VarChar,sjs.step_id) + ', @database_user_name = ''UserName''' AS OutputStatements
FROM dbo.sysjobsteps sjs
INNER JOIN dbo.sysjobs sj
ON sjs.job_id = sj.job_id

This will output something similar to this in your results window:

EXEC sp_update_jobstep @job_name = 'SQL MDW: Auto Index Management', @step_id = 1, @database_user_name = 'UserName'
EXEC sp_update_jobstep @job_name = 'SQL MDW: Auto Index Management', @step_id = 2, @database_user_name = 'UserName'
EXEC sp_update_jobstep @job_name = 'syspolicy_purge_history', @step_id = 3, @database_user_name = 'UserName'
EXEC sp_update_jobstep @job_name = 'syspolicy_purge_history', @step_id = 2, @database_user_name = 'UserName'
EXEC sp_update_jobstep @job_name = 'syspolicy_purge_history', @step_id = 1, @database_user_name = 'UserName'

Copy all the statements by selecting on the column header (OutputStatements) and hit CTRL+C. Open a new query window and paste the results CTRL+V into the query window. Remove any EXEC statement/lines you see for maintenance jobs or others you do not want to update with a database user.

Now you should be left with the sp_update_jobstep commands for those you truly want to update. Execute the query by hitting !Execute or F5.

To see your new job step details, use this statement:

SELECT sj.name, sjs.job_id, sjs.step_id, sjs.database_user_name
FROM dbo.sysjobsteps sjs
INNER JOIN dbo.sysjobs sj
    ON sjs.job_id = sj.job_id

Happy job step changing. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM