Skip navigation

Generating Scripts for SQL Server Jobs

Downloads
38790.zip

How can I generate scripts for all jobs running on my SQL Server? I want one script that includes all jobs and one script for each individual job.

You can use SQL Distributed Management Objects (SQL-DMO) to obtain a list of jobs. Use the JobServer object and iterate through the Jobs collection. To obtain each job's script, you can call the Scripts method for that job. Here's what you should do:

  1. Start Visual Basic (VB) 6.0.
  2. Start a standard EXE project.
  3. Add a command button.
  4. Double-click the new command button to get the click event code window.
  5. From the main VB menu, select Project, References. Scroll through the References list to find the Microsoft SQLDMO Object Library and add it. Try to use the .dll file that shipped with the version of SQL Server you're running the .dll against. You can find the .dll version by looking at the location the .dll comes from in the window you have open.
  6. To fetch all of your jobs, paste the code that Listing 4 shows into the click handler for the button. Review the Change if required lines, then run the program.

If you want to script jobs interactively, you can do so from Enterprise Manager by right-clicking jobs and choosing All Tasks, Generate SQL Script.

TAGS: SQL
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish