If anybody still looking for this then below should do. Not the finest code but gets the job done.
/*
Replace the below statement with the actual keyword to be replaced:
INSERT INTO SP_Keyword VALUES
('sys.databases3', 'sys.databases5')
,('sysjobs3', 'sysjobs5')
Here we are replacing sys.databases3 and sysjobs3
with sys.databases5 and sysjobs5 respectively.
Script only searches in 'TSQL', 'Powershell', 'CMDEXEC' job type.
*/
SET NOCOUNT ON
use msdb
GO
DECLARE @OldV VARCHAR(max)
DECLARE @NEWV VARCHAR(max)
DECLARE @job_id2 varchar(max)
DECLARE @step_id2 varchar(10)
DECLARE @command2 varchar(max)
DECLARE @command3 varchar(300)
--CLEANUP TASKS
IF EXISTS(SELECT * FROM sys.tables where name = 'SP_Keyword')
DROP TABLE SP_Keyword
CREATE TABLE SP_Keyword
(
old_keyword varchar(200),
new_keyword varchar(200)
)
--DEBUG STATEMENT
--PRINT 'SP_Keyword TABLE CREATED'
INSERT INTO SP_Keyword VALUES
('sys.databases3', 'sys.databases5')
,('sysjobs3', 'sysjobs5')
--DEBUG STATEMENT
--PRINT 'VALUES INSERTED FOR KEYWORD'
CREATE TABLE #jobs(
[name] [sysname] ,
[step_name] [sysname] ,
[subsystem] [varchar](40) ,
[command] [varchar](max) ,
[keyword] [varchar](200) ,
[database_name] [sysname] ,
[enabled] [tinyint] ,
[description] [varchar](512) ,
[date_created] [datetime] ,
[job_id] [uniqueidentifier] ,
[step_id] [int]
)
DECLARE db_cursor3 CURSOR FOR
SELECT old_keyword FROM SP_Keyword
OPEN db_cursor3
FETCH NEXT FROM db_cursor3 INTO @OldV
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #jobs
SELECT a.name as job_name, b.step_name, b.subsystem,
b.command, @OldV as 'keyword', 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
WHERE [command] LIKE '%'+@OldV+'%'
AND [subsystem] IN ('TSQL', 'Powershell', 'CMDEXEC')
FETCH NEXT FROM db_cursor3 INTO @OldV
END
CLOSE db_cursor3
DEALLOCATE db_cursor3
IF NOT EXISTS(SELECT * FROM #jobs)
BEGIN
PRINT 'NO JOBS FOUND TO BE REPLACED'
GOTO SKIPPER
END
--SELECT name as job_name, step_name, command, keyword, database_name, enabled, description, date_created FROM #jobs
DECLARE db_cursor3 CURSOR FOR
SELECT * FROM SP_Keyword
OPEN db_cursor3
FETCH NEXT FROM db_cursor3 INTO @OldV,@newV
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE #jobs SET command=REPLACE(command,@oldV,@NewV)
WHERE command LIKE '%'+@OldV+'%'
FETCH NEXT FROM db_cursor3 INTO @oldV,@NewV
END
CLOSE db_cursor3
DEALLOCATE db_cursor3
print 'COMMAND TO REPLACE JOB STEPS IS BEING EXECUTED'
DECLARE db_cursor3 CURSOR FOR
SELECT job_id, step_id, command FROM #jobs
OPEN db_cursor3
FETCH NEXT FROM db_cursor3 INTO @job_id2,@step_id2, @command2
WHILE @@FETCH_STATUS = 0
BEGIN
SET @command3 = 'sp_update_jobstep @job_id = '''+@job_id2+''' ,@step_id = '+@step_id2+', @command = '''+@command2+''''
PRINT @command3
EXEC(@command3)
FETCH NEXT FROM db_cursor3 INTO @job_id2,@step_id2, @command2
END
CLOSE db_cursor3
DEALLOCATE db_cursor3
SKIPPER:
DROP TABLE #jobs
DROP TABLE SP_Keyword
PRINT 'CLEANUP DONE'
PRINT 'PROGRAM COMPLETE'