Microsoft KB Archive/906547

= How to create a dynamic subject or message for the Send Mail task in SQL Server 2005 Integration Services =

Article ID: 906547

Article Last Modified on 3/11/2006

-

APPLIES TO


 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Enterprise Edition
 * Microsoft SQL Server 2005 Standard Edition

-





INTRODUCTION
You can create a Microsoft SQL Server 2005 Integration Services (SSIS) package by using SQL Server Business Intelligence Development Studio. When you create this package, you can create an expression for a property of the SSIS package to update or to populate the property at runtime. For example, if the SSIS package contains a Send Mail task, you can create an expression for the Subject property and for the MessageSource property. You can use the Subject property expression to dynamically update the subject of an e-mail message. You can use the MessageSource property expression to dynamically update variables in the e-mail message, such as variables that are populated by a Row Count transformation.

This article discusses how to create a dynamic subject or message for the Send Mail task.



MORE INFORMATION
The following is a sample property expression for the Subject property in a Send Mail task. &quot;Package>>> &quot; + @[System::PackageName] +&quot; was executed at>>> &quot; + (DT_WSTR, 40) @[System::StartTime] + &quot; by user>>> &quot; + @[System::UserName] + &quot; on Machine>>> &quot; + @[System::MachineName] If you use this sample property expression, the subject of an e-mail message is updated dynamically. The subject will include the following information:
 * Text information

In this example, the e-mail message subject includes the &quot;Package>>> &quot; text information.
 * System variables

The e-mail message includes the following system variables:
 * PackageName

The package name.
 * StartTime

The time that the package was executed.
 * UserName

The user who executed the package.
 * MachineName

The name of the computer on which the package was executed.

You can also include more information in the expression, such as a user-defined variable. For example, a Data Flow task can include a Row Count transformation before the Send Mail task. (The Row Count transformation is used to count rows.) The Row Count transformation populates a user-defined variable that is named @myrowcount. This variable stores the count information in the data flow.

To specify that an e-mail message be sent only if the row count is smaller than a certain value, modify the control flow by using precedence constraints. To do this, follow these steps:  In SQL Server Business Intelligence Development Studio, right-click Data Flow Task, and then click Add Precedence Constraint. Double-click the precedence constraint that you just created. In the Precedence Constraint Editor dialog box, click Expression and Constraint in Evaluation operation. In the Expression box, type the following expression:

@myrowcount < 2

 In the Precedence Constraint Editor dialog box, click OK.

If less than two rows are processed in the data flow, an e-mail message is sent.

Additionally, you can use the Send Mail task as part of an error handler. For example, you may want to send an e-mail message to administrators when an SSIS package does not execute. To do this, create an OnError event handler for the package, and then add a Send Mail task to the event handler. Create a subject property expression that captures the time that the package is executed, the start time of the container, or the start time of the event handler from the relevant system variables. For example, create an expression that is similar to the following. &quot;Error in the task: &quot; + @[System::SourceName] + &quot;with the ID: &quot; +  @[System::SourceID] + &quot; has failed at: &quot; + (DT_WSTR, 20) @[System::ContainerStartTime] + &quot;.&quot; This sample expression uses the following system variables:
 * StartTime

The time when the package was executed.
 * ContainerStartTime

The time that the container started.
 * EventHandlerStartTime

The time that the event handler started.

