我想从 SQL Server 2014 的作业 (ETL) 中提取代码,这些代码位于 SQL Server 代理 -> 作业中。我目前有 400 多个,有没有比输入每个并保存代码更快的方法?
我尝试了 ChatGPT 或 Gemini 的各种代码,但它不起作用,它没有显示我在每个作业(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;