Reputation: 405
I want to extract the code from my Jobs (ETL) from my SQL Server 2014, which are in SQL Server Agent -> Jobs. I currently have more than 400, is there a faster way than entering each one and saving the code?
I have tried various codes from ChatGPT or Gemini and it does not work, it does not show me the codes that I have in each Job (ETL)
DECLARE @jobname sysname, @filename varchar(255), @job_id uniqueidentifier, @sql nvarchar(max);
DECLARE job_cursor CURSOR FOR
SELECT name, job_id FROM msdb.dbo.sysjobs;
OPEN job_cursor;
FETCH NEXT FROM job_cursor INTO @jobname, @job_id;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @filename = CONCAT('C:\Scripts\Jobs\', @jobname, '.sql');
-- Construct the command to generate the script of the job
SET @sql = 'EXEC msdb.dbo.sp_helpjob @job_id = ''' + CAST(@job_id AS varchar(36)) + ''';';
-- Execute the command and redirect the output to a file, using Windows authentication
EXEC xp_cmdshell CONCAT('sqlcmd -S your_server_name -d msdb -Q "', @sql, '" -o "', @filename, '"');
FETCH NEXT FROM job_cursor INTO @jobname, @job_id;
END
CLOSE job_cursor;
DEALLOCATE job_cursor;
Upvotes: 0
Views: 92
Reputation: 5157
In SSMS you can select multiple Jobs to be "extracted" into a single script:
P.S. This trick works for many things
Upvotes: 0