How to mass update all or some sql server agent job step commands
How to mass update all or some sql server agent job step and commands using TSQL. This will use the built-in Replace function to search text in SQL agent jobs
SQL Agent Job information is stored in MSDB and can reviewed using some of the following queries:
SELECT * FROM msdb.dbo.SysJobSteps
-- OR
SELECT a.name as job_name, b.step_name, b.subsystem, b.command, b.database_name, a.enabled
,a.description, a.date_created, b.job_id, b.step_id
FROM [dbo].[sysjobsteps] as b
INNER JOIN sysjobs as a ON a.job_id = b.job_id
Since I will be focusing on the Commands column I will look at using this query to extract data into a temp table so that I can test any updates with the replace step before doing an update to MSDB.
This would be your opportunity to archive or save any commands as needed. Some people may prefer to generate all the syntax necessary to use EXEC sp_update_jobstep so you are welcome to rewrite my update statement block to use a CURSOR and While loop as needed. In the below example I am simply looking for ; Some.Email@Email.net and replacing it by blanking it out. Additionally I wrapped the update in an IF block so that if no results return you don't do any unnecessary work.
-- Script only searches Agent Job Types of 'TSQL', 'Powershell', 'CMDEXEC' for Update
-- Replace the below values with the actual keyword(s) to be replaced:
-- @OldV will be the OLD Value only update the items within the ticks
-- @NEWV will be the NEW Value You need to have the ticks ''
-- ** You will need to remove the -- around the update statement to make the actual update **
-----------------------------------------------------------------------------------------
SET NOCOUNT ON;
USE [msdb];
GO
DECLARE @OldV VARCHAR(max);
DECLARE @NEWV VARCHAR(max);
SET @OldV = '; Some.Email@Email.net';
SET @NEWV = '';
SELECT a.name as job_name, b.step_name, b.subsystem, b.command, @OldV as 'Old Value', b.database_name, a.enabled
,a.description, a.date_created, b.job_id, b.step_id
INTO #jobs FROM [dbo].[sysjobsteps] as b
INNER JOIN sysjobs as a ON a.job_id = b.job_id
WHERE b.command LIKE '%' + @OldV + '%'
AND b.subsystem IN ( 'TSQL', 'Powershell', 'CMDEXEC' );
--SELECT name as job_name, step_name, command, keyword, database_name, enabled, description, date_created FROM #jobs
print 'SHOW COMMANDS IN JOB STEPS BEFORE ANY CHANGES';
SELECT * FROM #jobs;
UPDATE #jobs
SET command = REPLACE( command, @oldV, @NewV )
WHERE command LIKE '%' + @OldV + '%';
print 'SHOW COMMANDS IN JOB STEPS AFTER UPDATE';
SELECT * FROM #jobs;
-- IF EXISTS (SELECT * FROM #jobs)
-- BEGIN
-- PRINT 'JOBS FOUND REPLACE Starting';
-- UPDATE b
-- SET b.command = REPLACE( b.command, @oldV, @NewV )
-- FROM [MSDB].[dbo].[sysjobsteps] as b
-- INNER JOIN [MSDB].[dbo].[sysjobs] as a ON a.job_id = b.job_id
-- WHERE b.command LIKE '%' + @OldV + '%'
-- AND b.subsystem IN ( 'TSQL', 'Powershell', 'CMDEXEC' );
-- END;
PRINT 'WORK All DONE';
DROP TABLE #jobs;
This comment has been removed by the author.
ReplyDeleteHow To Mass Update All Or Some Sql Server Agent Job Step Commands >>>>> Download Now
ReplyDelete>>>>> Download Full
How To Mass Update All Or Some Sql Server Agent Job Step Commands >>>>> Download LINK
>>>>> Download Now
How To Mass Update All Or Some Sql Server Agent Job Step Commands >>>>> Download Full
>>>>> Download LINK Ww