Chapter 10: Execute SQL Server Agent Job Task 136 For example, you may have a custom job that: • checks disk space and sends you a warning if it's getting near full • kicks off a SQL Trace script to capture trace data on a scheduled basis • checks a particular value, or values, in a DMV that you are interested in monitoring, and sends you an alert if the value(s) exceeds a predefined threshold • starts a job that copies local MDF and LDF backups from off the local SQL Server instance to another server location (preferably offsite). As discussed in the introduction, if you find yourself wanting to add multiple SQL Server Agent jobs to the plans you're creating using the Wizard, you're probably better off avoiding the Wizard in the first place, and using T-SQL or PowerShell scripting. When the Execute SQL Server Agent Job task runs, T-SQL similar to the following is executed. EXEC msdb.dbo.sp_start_job @job_id=N'cb73ea96-9a96-49fe-ada9- a70a941f9fb9' Notice that this is the execution of a system-stored procedure, which is instructed to run a job with a specified internal number. The number is not very useful to us, but if you want to look it up, in order to find out exactly what job was run (assuming you don't know), you could run the following SELECT statement: SELECT * FROM msdb.dbo.sysjobs_view This query will display all job IDs, along with their job names, so you can easily identify which job ID matches which job name. When an Execute SQL Server Agent Job task runs, it produces a text report similar to the following: Microsoft(R) Server Maintenance Utility (Unicode) Version 10.0.2531 Report was generated on "HAWAII." Maintenance Plan: MaintenancePlan Duration: 00:00:00 Status: Succeeded. Details: Execute SQL Server Agent Job (HAWAII) Execute Job on Local server connection Job name: Send Alert If Disk Space Exceeds 80% of Capacity Task start: 2009-07-30T16:06:17. Task end: 2009-07-30T16:06:17. Chapter 10: Execute SQL Server Agent Job Task 137 Success Command:EXEC msdb.dbo.sp_start_job @job_id=N''cb73ea96-9a96-49fe- ada9-a70a941f9fb9'' GO Given that the Execute SQL Server Agent Job task can only run a single job, this report is not very long. You may want to note that the name of the job is included in the report, which can make it easier to troubleshoot potential problems with this task, should one arise. When and How Often to Run the Custom Job When and how often you run this job will depend on what the SQL Server Agent job does. If you are running a very lightweight job, such as checking disk space, you can run it almost whenever you want, and as often as you want. On the other hand, if the SQL Server Agent job uses a lot of server resources to run, and/or takes a long time to run, then you will have to schedule the job so that it doesn't interfere with users accessing the database, or with the running of other jobs. Ideally, you will schedule it during available maintenance windows. Creating SQL Server Agent Jobs If you decide to use the Execute SQL Server Agent Job task, you'll first need to create and configure the custom SQL Server Agent job that you want to run as part of your plan. How to create a SQL Server Agent job is beyond the scope of this book, but is explained in Books Online. You'd create the job just like you would any other SQL Server Agent job, except that you won't schedule it, as you will use the Maintenance Plan Wizard to do the scheduling of the job for you. In addition, you will want to ensure that the SQL Server Agent job has been properly created and works as expected before you add it to a Maintenance Plan. Chapter 10: Execute SQL Server Agent Job Task 138 Configuring the Execute SQL Server Agent Job Task The Define Execute SQL Server Agent Job Task screen is very straightforward. Your only options are to choose the single job that you want to execute as part of the plan, and schedule when the job is to run. Selecting the Job Figure 10.1 shows some the available SQL Server Agent jobs on my server. Every SQL Server Agent job on the server will be displayed, and you may have to scroll down to see them all. If you don't see any SQL Server Agent jobs listed in the Wizard, then you haven't created any, and should refer to the previous section! Notice, in Figure 10.1, that there are checkboxes next to each job, giving the impression that you should be able to select multiple jobs from this screen and run them as part of your plan. This is not the case. If you select one checkbox, then another, the first checkbox you selected is deselected and only the new one remains selected. If Microsoft was following its own user interface design guidelines, there would be radio buttons here instead of checkboxes. For this example, I've selected the Send Alert If Disk Space Exceeds 80% of Capacity job, as the SQL Server Agent job I want included as part of my Maintenance Plan. Chapter 10: Execute SQL Server Agent Job Task 139 Figure 10.1: You can only select one SQL Server Agent job to execute as part of a Maintenance Plan. Creating the Job Schedule The next, and last step, as always, is to schedule when the task should run, using the Schedule option. Scheduling this task is like scheduling all the other tasks using the Maintenance Plan Wizard. Ideally, the job should run during a maintenance window, or at least during a slower time of the day, and it should not overlap other plan tasks. Your scheduling, of course, will have to take into account the resource impact of the SQL Server Agent job. If the job is lightweight, such as checking disk space, then you have great flexibility when running it. But if the job is heavyweight, and uses a lot of SQL Server resources, then you will have to be much more careful about scheduling when, and how often, it runs. Chapter 10: Execute SQL Server Agent Job Task 140 Summary In theory, the Execute SQL Server Agent Job task is designed to add a little bit of flexibility to Maintenance Plans created using the Maintenance Plan Wizard. As long as you keep any Execute SQL Server Agent Job task simple and lightweight, you shouldn't run into any problems. On the other hand, it is important not to misuse this feature, and try to make it perform tasks it is not really designed to do. If you need database maintenance functionality that does not exist inside the Wizard, then take my advice (which by now may seem to be a little repetitive), and consider performing your database maintenance using T-SQL or PowerShell scripts instead. In the next chapter, we learn about the History Cleanup task, which performs an important function many DBAs forget they need to carry out. . available maintenance windows. Creating SQL Server Agent Jobs If you decide to use the Execute SQL Server Agent Job task, you'll first need to create and configure the custom SQL Server Agent. available SQL Server Agent jobs on my server. Every SQL Server Agent job on the server will be displayed, and you may have to scroll down to see them all. If you don't see any SQL Server. the SQL Server Agent job has been properly created and works as expected before you add it to a Maintenance Plan. Chapter 10: Execute SQL Server Agent Job Task 138 Configuring the Execute SQL Server