Although the main tasks of SSIS packages exist in either the Control Flow or the Data Flow, the Event Handlers tab, the subject of this lesson, is another tab that is very useful when you are creating a package. Event handlers enable you to call tasks when events like errors or warnings occur during the execution of a package, which can be helpful in logging errors or sending notifications. The Event Handlers tab is used to call a task when an event has occurred in the package. The tasks in the event handler run only if the proper event is called in the package. Therefore, it is possible to have tasks in the event handlers of a package that don’t run during the execution of the package because the proper event is not called.
Several events can occur in a package to cause an event to fire and call an event handler:
The most commonly used events are OnError, OnWarning, OnPreExecute, and OnPostExecute.
When you open the Event Handlers tab for the first time, it looks similar to Figure 48-1. Notice there is a blue link in the middle. When clicked, this link creates an event handler on the package. At the top of the Event Handlers tab are two drop-down menus. The left drop-down menu contains a list of all the executables in the package. This is a list of all the tasks in the package. If no tasks have been created in the package, the only executable listed is the package.
When you are creating an event handler, it is important to select the proper executable from the drop-down menu to ensure the tasks in the event handler execute when intended.
The second drop-down menu, on the top right of the Event Handlers tab, contains a list of all the events that can be chosen for the selected executable.
In Figure 48-2, there is a Script Task in the Control Flow of the package and a Data Flow with a source and destination. Notice the source and destination do not show in the drop-down menu of executables. Rather, the entire Data Flow is the executable. Placing an OnPostExecute Event Handler on the Data Flow means the task in the event handler will fire after the entire Data Flow finishes executing. Placing an OnError Event Handler on the Script Task means the tasks in the event handler will fire only if an error occurs on the Script Task. So, for example, if no error occurs or if an error occurs in the Data Flow (as opposed to in the Script Task), the OnError event on the Script Task will not fire.
When you first open the Event Handlers tab and click the blue link in the middle of the tab, it creates an OnError event for the package. This causes the tasks in the event handler to execute if any errors occur during any tasks in the package. Sometimes you may want the tasks in the event handlers to fire only for a certain task in the package. To do this, first select the task in the left drop-down menu and then click the blue link in the event handler tab. This action creates an event handler for the specific tasks and executes only when the proper event occurs on the selected tasks.
Two of the most common uses for the event handlers are notification and logging. When you want to be notified via e-mail that an error has occurred, the event handlers are the right place to execute this task. Simply place a Send Mail Task in the OnError Event Handler for the package. When you want to be notified via e-mail that a package has completed, again the event handlers are the right place to execute this task. Just place a Send Mail Task in the OnPostExecute Event Handler for the package.
These Send Mail Tasks can contain information about the package. You can include any system variables in the message to tell you what occurred in the package and when.
Another useful purpose of the event handlers is logging. You can create your own custom logging framework if you are not satisfied with the built-in logging of SSIS. By creating Execute SQL Tasks in the event handlers, you can write information from the package to a database. These Execute SQL Tasks execute only when the proper event occurs, enabling you to log errors when they occur with the OnError Event Handler and log warnings when they occur with the OnWarning Event Handler.
After several event handlers have been created and these event handlers are on different executables, you might find it hard to keep track of which executable has an event handler and which does not. The Executable drop-down menu shows all of the event handlers on the package and each executable. There is also an Event Handler folder under the package and each executable. Clicking the plus sign next to this folder opens the folder and shows the event handlers in each of them.
Under the event handler is an Executables folder showing each task in the event handler. In Figure 48-3, you can see the package has three event handlers, each with an Execute SQL Task. The Data Flow and Script Task have OnError Event Handlers with Send Mail Tasks in them. You can also see this information in the Package Explorer tab.
When you click the plus sign next to any task in an event handler, you see there is another Event Handler folder. The task in the event handler can have an event handler of its own. So, you can have a Send Mail Task in the OnPostExecute Event Handler of a package and then have an Execute SQL Task in the OnError Event Handler of the Send Mail Task. If the Send Mail Task has an error, the error can then be logged with the Execute SQL Task.
In this Try It, you create a package with an Execute SQL Task with an OnError Event Handler. The event handler is going to cause a pop-up message from a Script Task to show the error. After this lesson, you will understand how to create and use event handlers on a package.
The completed package for this lesson is available for download on the book’s website at www.wrox.com.
Create a package with an Execute SQL Task. Run it with no error, and run it with an error. The OnError Event Handler should fire when an error occurs.
MsgBox(Dts.Variables(“ErrorDescription“).Value)