sethreno/schemazen

What about scripting jobs?

Opened this issue · 3 comments

Jobs are stored in the msdb database, but inside tables, not as code explicitly. I run this snippet to get their code, but wonder if there's something better (and not doing so by hand in SSMS, one by one)

  SELECT [type] = 'JOB', [object] = J.name + ' - Step ' + convert(varchar,S.step_id) + ': ' + S.step_name, code = S.command
    FROM [msdb].[dbo].[sysjobsteps] S
   INNER JOIN [msdb].[dbo].[sysjobs] J
      ON S.job_id = J.job_id
   WHERE database_name = DB_NAME()

here's a snippit from a larger Powershell process I have to script off jobs to disk...assumes $ServerName and $OutputFolder are set....dumps it all to one file...you could pipe each occurrence to its own file as well:

#Create a new SMO instance for this $ServerName
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $ServerName
#Script out each SQL Server Agent Job for the server to a file with no changes as a baseline
$srv.JobServer.Jobs | foreach {$_.Script() + "GO`r`n"} | out-file "$OutputFolder\jobs$(get-date -f yyyyMMdd_HHmmss).sql"

great! thanks @jheimx4 , i'll give a try

You could also use something like BCP to copy data in and out of job fields, I dont really think schemazen would want to go to separate databases and script out objects outside of the target.

https://docs.microsoft.com/en-us/sql/relational-databases/import-export/import-and-export-bulk-data-by-using-the-bcp-utility-sql-server?view=sql-server-ver15