Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about scripting jobs? #169

Open
bernardoadc opened this issue May 30, 2019 · 3 comments
Open

What about scripting jobs? #169

bernardoadc opened this issue May 30, 2019 · 3 comments

Comments

@bernardoadc
Copy link

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()
@jheimx4
Copy link

jheimx4 commented Sep 11, 2019

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"

@bernardoadc
Copy link
Author

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

@ConstantineK
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants