Generating Scripts for SQL Server Jobs

SQL-DMO gives you a way to list jobs running on SQL Server and obtain their scripts.

ITPro Today logo in a gray background | ITPro Today


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.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like