20767A - Implementing A SQL Data Warehouse-195-230
20767A - Implementing A SQL Data Warehouse-195-230
Module 7
Implementing Control Flow in an SSIS Package
Contents:
Module Overview 7-1
Module Overview
You can use control flow in SQL Server Integration Services (SSIS) packages to implement complex extract,
transform, and load (ETL) solutions that combine multiple tasks and workflow logic. By learning how to
implement control flow, you can design robust ETL processes for a data warehousing solution that
coordinate data flow operations with other automated tasks.
Objectives
After completing this module, you will be able to:
Implement control flow with tasks and precedence constraints.
Lesson 1
Introduction to Control Flow
Control flow in an SSIS package consists of one or more tasks, usually executed as a sequence, based on
precedence constraints that define a workflow. Before you can implement a control flow, you need to
know what tasks are available and how to define a workflow sequence using precedence constraints. You
should also understand how to use multiple packages to create complex ETL solutions.
Lesson Objectives
After completing this lesson, you will be able to:
Use design time features of SSIS to help you develop control flow efficiently.
Data Flow Encapsulates a data flow that transfers data from a source to a
destination.
Database Tasks
Bulk Insert Inserts data into a data destination in a bulk load operation.
CDC Control Performs a change data capture (CDC) status management operation.
CDC is discussed in Module 7: Implementing an Incremental ETL
Process.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-3
File System Performs file system operations, such as creating folders or deleting
files.
FTP Performs file transfer protocol (FTP) operations, such as copying files.
Message Queue Sends and receives MSMQ messages between an SSIS package and an
Application Queue.
WMI Tasks
Script Runs Microsoft Visual Studio® Tools for Applications (VSTA) script
written in Microsoft Visual Basic or Microsoft Visual C#.
Transfer Database Transfers a database from one SQL Server instance to another.
Transfer Error Transfers custom error messages from one SQL Server instance to
Messages another.
Transfer Jobs Transfers SQL Agent jobs from one SQL Server instance to another.
Transfer Logins Transfers logins from one SQL Server instance to another.
Transfer Master Stored Transfers stored procedures in the master database from one SQL
Procedures Server instance to another.
Transfer SQL Server Transfers database objects, such as tables and views, from one SQL
Objects Server instance to another.
Analysis Services Runs a data definition language (DDL) statement in an Analysis Services
MCT USE ONLY. STUDENT USE PROHIBITED
7-4 Implementing Control Flow in an SSIS Package
Analysis Services Processes an Analysis Services object, such as a cube or data mining
Processing model.
Data Mining Query Runs a prediction query using a data mining model.
History Cleanup Deletes out-of-date history data for SQL Server maintenance
operations.
Notify Operator Sends a notification by email message, pager message, or network alert
to an SQL Agent operator.
Shrink Database Reduces the size of the specified SQL Server database.
Update Statistics Updates value distribution statistics for tables and views in an SQL
Server database.
Note: The Execute T-SQL Statement task can only be used to query a Microsoft SQL Server
Database and lacks the flexibility of the Execute SQL task. If you need to query a database
produced by another vendor, run parameterized queries, or assign the results of a query to
variables, you should use the Execute SQL task instead.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-5
Precedence Constraints
A control flow usually defines a sequence of tasks to
be executed. You define the sequence by
connecting tasks with precedence constraints. These
constraints evaluate the outcome of a task to
determine the flow of execution.
Completion – The execution flow to follow when a task completes, regardless of whether it succeeds
or fails. In the control flow designer, complete constraints are shown as black arrows.
By using these conditional precedence constraints, you can define a control flow that executes tasks based
on conditional logic. For example, you could create a control flow with the following steps:
1. An FTP task downloads a file of sales data to a local folder.
2. If the FTP download succeeds, a Data Flow task imports the downloaded data into an SQL Server
database. However, if the FTP download fails, a Send Mail task notifies an administrator that there is a
problem.
3. When the Data Flow task completes, regardless of whether it fails or succeeds, a File System Task
deletes the folder where the customer data file was downloaded.
You can connect multiple precedence constraints to a single task. For example, a control flow might
include two Data Flow tasks, and a Send Mail task that you want to use to notify an administrator if
something goes wrong. To accomplish this, you could connect a failure precedence constraint from each
of the Data Flow tasks to the Send Mail task. However, you need to determine whether the notification
should be sent if either one of the Data Flow tasks fails, or only if both fail.
By default, when multiple precedence constraints are connected to a single task, a logical AND operation
is applied to the precedence condition, meaning that all the precedence constraints must evaluate to True
to execute the connected task. In the example above, this means that the Send Mail task would only
execute if both Data Flow tasks fail. In the control flow designer, logical AND constraints are shown as
solid arrows.
You can double-click a precedence constraint to edit and configure it to use a logical OR operation, in
which case the connected task executes if any of the connections evaluates to True. Setting the constraints
in the example above to use a logical OR operation would result in the Send Mail task executing if either
(or both) of the Data Flow tasks fails. In the control flow designer, logical OR constraints are shown as
dotted arrows.
MCT USE ONLY. STUDENT USE PROHIBITED
7-6 Implementing Control Flow in an SSIS Package
Grouping Tasks
Show or hide the individual tasks to make the best use of space on the screen.
To create a group of tasks, select the ones you want by dragging around or clicking them while holding
the CTRL key. Right-click any of the selected tasks and then click Group.
Adding Annotations
You can add annotations to the design surface to document your workflow. An annotation is a text-based
note that you use to describe important features of your package design. To add an annotation, right-
click the design surface, click Add Annotation, and then type the annotation text.
Note: You can add annotations to the Control Flow design surface, the Data Flow design
surface, and the Event Handler design surface.
Demonstration Steps
Add Tasks to a Control Flow
1. Ensure that the 20767A-MIA-DC and 20767A-MIA-SQL virtual machines are both running, and then
log on to 20767A-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd.
3. Start SQL Server Data Tools and open ControlFlowDemo.sln from the D:\Demofiles\Mod07
folder.
4. In Solution Explorer, double-click Control Flow.dtsx.
5. If the SSIS Toolbox is not visible, on the SSIS menu, click SSIS Toolbox. Then, from the SSIS Toolbox,
drag a File System Task to the control flow surface.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-7
6. Double-click the File System Task, configure the following settings, and then click OK.
o SourceConnection: A new connection with a Usage type of Create folder, and a Folder value
of D:\Demofiles\Mod07\Demo. (Do not create this folder)
7. From the SSIS Toolbox, drag a second File System Task to the control flow surface. Then double-click
the File System Task, and then configure the following settings, and then click OK.
o SourceConnection: Demo
8. From the SSIS Toolbox, drag a third File System Task to the control flow surface. Then double-click
File System Task, configure the following settings and then click OK.
o SourceConnection: Demo
9. From the SSIS Toolbox, drag a fourth File System Task to the control flow surface. Then double-click
the File System Task, configure the following setting, and then click OK.
o OverwriteDestination: True
o SourceConnection: A new connection with a Usage type of Existing file, and a File value of
D:\Demofiles\Mod07\Demo.txt
10. From the SSIS Toolbox, drag a Send Mail Task to the control flow surface. Then double-click the
Send Mail Task, configure the following settings, and then click OK.
o SmtpConnection (on the Mail tab): Create a new SMTP connection manager with a Name
property of Local SMTP Server and an SMTP Server property of localhost. Use the default
values for all other settings
1. Select the Delete Files task and drag its green arrow to the Delete Folder task. Then connect the
Delete Folder task to the Create Folder task and the Create Folder task to the Copy File task.
2. Connect each of the File System tasks to the Send Failure Notification task.
3. Right-click the connection between Delete Files and Delete Folder, and then click Completion.
MCT USE ONLY. STUDENT USE PROHIBITED
7-8 Implementing Control Flow in an SSIS Package
4. Right-click the connection between Delete Folder and Create Folder and click Completion.
5. Click the connection between the Delete Files task and the Send Failure Notification task to select
it. Then hold the Ctrl key and click each connection between the remaining File System tasks and the
Send Failure Notification task while holding the Ctrl key to select them all.
6. Press F4 and in the Properties pane, set the Value property to Failure.
7. Click anywhere on the control flow surface to clear the current selection, and then double-click any of
the red constraints connected to the Send Failure Notification task. Then in the Precedence
Constraint Editor dialog box, in the Multiple constraints section, click Logical OR. One constraint
must evaluate to True, and click OK. Note that all connections to the Send Failure Notification
task are now dotted to indicate that a logical OR operation is applied.
8. Right-click the control flow surface next to the Send Failure Notification task and click Add
Annotation. Then type Send an email message if a task fails.
9. Select the Delete Files and Delete Folder tasks, then right-click either of them and click Group. Drag
the group to rearrange the control flow so you can see that the Delete Folder task is still connected
to the Create Folder task.
10. On the Debug menu, click Start Debugging to run the package, and note that the Delete Files and
Delete Folder tasks failed because the specified folder did not previously exist. This caused the Send
Failure Notification task to be executed.
11. You can view the email message that was sent by the Send Failure Notification task in the
C:\inetpub\mailroot\Drop folder. Double-click it to open with Outlook.
12. In SQL Server Data Tools, on the Debug menu, click Stop Debugging, and then run the package
again. This time, all the File System tasks should succeed because the folder was created during the
previous execution. Consequently, the Send Failure Notification task is not executed.
13. Stop debugging and close SQL Server Data Tools. Save the solution files if prompted.
o Connection Managers
o Tasks
o Event Handlers
3. Change the Name and ID properties of the new package to avoid naming conflicts.
Which of the following is one of the key advantages of developing multiple packages
within an SSIS solution?
It creates reusable units of workflow which you can use multiple times in an ETL
process.
Multiple small packages use less resources than a single large package.
MCT USE ONLY. STUDENT USE PROHIBITED
7-10 Implementing Control Flow in an SSIS Package
Lesson 2
Creating Dynamic Packages
You can use variables, parameters, and expressions to make your SSIS packages more dynamic. For
example, rather than hard-coding a database connection string or file path in a data source, you can
create a package that sets the value dynamically at run time. This produces a more flexible and reusable
solution, helping to mitigate differences between the development and production environments.
This lesson describes how you can create variables and parameters, and then use them in expressions.
Lesson Objectives
After completing this lesson, you will be able to:
Variables
You can use variables to store values that a control
flow uses at run time. Variable values can change
when you execute the package to reflect run-time
conditions. For example, a variable storing a file
path might change, depending on the specific
server on which the package is running. You can
use variables to:
User Variables
You can define user variables to store dynamic values that your control flow uses. To create a variable,
view the Variables pane in SSIS Designer and click Add Variable. For each user variable, you can specify
the following properties:
Name – A name for the variable. The combination of name and namespace must be unique within
the package. Note that variable names are case-sensitive.
Scope – The scope of the variable. Variables can be accessible throughout the whole package, or
scoped to a particular container or task. You cannot set the scope in the Variable pane; it is
determined by the object selected when you create the variable.
Data Type – The type of data the variable will hold; for example string, datetime, or decimal.
Namespace – The namespace in which the variable name is unique. By default, user variables are
defined in the User namespace, but you can create additional namespaces as required.
Raise Change Event – A true/false value specifying whether to raise an event when the variable value
changes. You can then implement an event handler to perform some custom logic.
IncludeInDebugDump – A true/false value specifying whether to include the variable value in debug
dump files.
Note: If you inadvertently create a variable with the wrong object selected, resulting in an
incorrect scope, you can change the scope of the variable by selecting it in the Variables window
and clicking Move Variable.
System Variables
System variables store information about the running package and its objects, and are defined in the
System namespace. Some useful system variables include:
UserName – The account name of the user who started the package.
Note: For a full list of system variables, see the SQL Server Integration Services
documentation in SQL Server Books Online.
System Variables
http://aka.ms/scqkpo
Parameters
You can use parameters to pass values to a project
or package at run time. When you define a
parameter, you can set a default value, which can
be overridden when the package is executed in a
production environment. For example, you could
use a parameter to specify a database connection
string for a data source, using one value during
development and a different value when the project
is deployed to a production environment.
Server default value – A default value assigned to the parameter during deployment. This value
overrides the design default value.
MCT USE ONLY. STUDENT USE PROHIBITED
7-12 Implementing Control Flow in an SSIS Package
Execution value – A value for a specific execution of a package. This value overrides both the server
and design default values.
When you deploy the project to an SSIS Catalog, you can define multiple environments and specify server
default parameter values for each environment.
Project parameters, which are defined at the project level and can be used in any packages within
the project.
Package parameters, which are scoped at the package level and are only available within the
package for which they are defined.
Note: Parameters are only supported in the project deployment model. When using the
legacy deployment model, you can set dynamic package properties by using package
configurations. Deployment is discussed in Module 12: Deploying and Configuring SSIS Packages.
Expressions
SSIS provides a rich expression language that you
can use to set values for numerous elements in an
SSIS package, including:
Properties.
For example, you could use an expression in a Data Flow task to specify the location of a file to be used as
a data source.
The following sample code shows an expression that concatenates a parameter containing a folder path
and a variable containing a file name to produce a full file path:
An SSIS Expression
@[$Project::folderPath]+[@User::fName]
Note that you must prefix variable names with an @ symbol and, to support identifiers with names
containing spaces, you use square brackets to enclose identifier names. Also, note that the expression uses
fully-qualified parameter and variable names, including the namespace, and that you prefix the parameter
name with a $ symbol.
You can type expressions or, in many cases, create them using the Expression Builder. You can use this
graphical tool to drag variables, parameters, constants, and functions to build up the expression that you
require. The Expression Builder automatically adds prefixes and text qualifiers for variables and
parameters, simplifying the task of creating complex expressions.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-13
Create a variable.
Create a parameter.
Use variables and parameters in an expression.
Demonstration Steps
Create a Variable
2. Start SQL Server Data Tools and open the VariablesAndParameters.sln solution in the
D:\Demofiles\Mod07 folder.
5. In the Variables pane, click Add Variable and add a variable with the following properties:
o Name: fName
Create a Parameter
2. In the Project.params [Design] pane, click Add Parameter and add a parameter with the following
properties:
o Name: FolderPath
o Data type: String
o Value: D:\Demofiles\Mod07\Files\
o Sensitive: False
o Required: True
1. On the Control Flow.dtsx package design surface, in the Connection Managers pane, click the
Demo.txt connection manager, and then press F4.
2. In the Properties pane, in the Expressions property box, click the ellipsis (…) button. In the Property
Expressions Editor dialog box, in the Property box, select ConnectionString and in the Expression
box, click the ellipsis (…) button.
MCT USE ONLY. STUDENT USE PROHIBITED
7-14 Implementing Control Flow in an SSIS Package
3. In the Expression Builder dialog box, expand the Variables and Parameters folder, and then drag
the $Project::folderPath parameters to the Expression box. In the Expression box, type a plus (+)
symbol and then drag the User::fName variable to the Expression box to create the following
expression:
@[$Project::folderPath]+[@User::fName]
4. In the Expression Builder dialog box, click Evaluate Expression and verify that the expression
produces the result D:\Demofiles\Mod07\Files\Demo1.txt.
5. Click OK to close the Expression Builder dialog box, and then in the Property Expressions Editor
dialog box, click OK.
6. Run the project, and when it has completed, stop debugging and close SQL Server Data Tools.
7. View the contents of the D:\Demofiles\Mod07\Demo folder and verify that Demo1.txt has been
copied.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-15
Lesson 3
Using Containers
You can create containers in SSIS packages to group related tasks together or define iterative processes.
Using containers in packages helps you create complex workflows and a hierarchy of execution scopes
that you can use to manage package behavior.
This lesson describes the kinds of containers that are available and how to use them in an SSIS package
control flow.
Lesson Objectives
After completing this lesson, you will be able to:
Use a For Loop container to repeat a process until a specific condition is met.
Use a Foreach Loop container to process items in an enumerated collection.
Introduction to Containers
SSIS packages can contain the following kinds of
containers:
Foreach Loop containers – You can use a Foreach Loop container to perform an iterative task that
processes each element in an enumerated collection. For example, you could use a Foreach Loop
container to execute a Data Flow Task that imports data from each file in a specified folder into a
database.
Containers can be start or endpoints for precedence constraints and you can nest containers within other
containers.
MCT USE ONLY. STUDENT USE PROHIBITED
7-16 Implementing Control Flow in an SSIS Package
Sequence Containers
You can use a Sequence container to group tasks
and other containers together, and define a subset
of the package control flow. By using a Sequence
container, you can:
Demonstration Steps
Use a Sequence Container
4. Right-click the Group indicator around the Delete Files and Delete Folder tasks, and then click
Ungroup to remove it.
5. Drag a Sequence Container from the SSIS Toolbox to the control flow design surface.
6. Right-click the precedence constraint that connects Delete Files to Send Failure Notification, and
click Delete. Then delete the precedence constraints connecting the Delete Folder to Send Failure
Notification and Delete Folder to Create Folder.
7. Click and drag around the Delete Files and Delete Folder tasks to select them both, and then drag
into the Sequence Container.
8. Drag a precedence constraint from the Sequence Container to Create Folder. Then right-click the
precedence constraint and click Completion.
9. Drag a precedence constraint from the Sequence Container to Send Failure Notification. Then
right-click the precedence constraint and click Failure.
10. Run the package and view the results, then click stop debugging.
11. Click the Sequence container and press F4. Then in the Properties pane, set the Disable property to
True.
12. Run the package again and note that neither of the tasks in the Sequence container is executed. Then
stop debugging and close SQL Server Data Tools.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-17
An evaluation expression that typically evaluates a counter variable, to exit the loop when it matches
a specific value.
To use a For Loop container in a control flow, drag the For Loop Container icon from the SSIS Toolbox to
the control flow surface, and then double-click it to set the expression properties required to control the
number of loop iterations. Drag the tasks and containers you want to repeat into the For Loop container
on the control flow surface.
Demonstration Steps
Use a For Loop Container
1. Ensure you have completed the previous demonstrations in this module.
2. Start SQL Server Data Tools and open the ForLoopContainer.sln solution in the
D:\Demofiles\Mod07 folder.
4. If the Variables window is not open, on the View menu, point to Other Windows, and click then
Variables. Then add a variable with the following properties:
o Name: counter
o Value: 0
5. From the SSIS Toolbox, drag a For Loop Container to the control flow design surface.
6. Double-click the For Loop Container, set the following properties, and then click OK:
o InitExpression: @counter = 1
7. From the SSIS Toolbox, drag an Execute Process Task into the For Loop container.
8. Double-click the Execute Process Task, set the following properties, and then click OK:
o Expressions (on the Expressions tab): Use the Property Expressions Editor to set the following
expression for the Arguments property:
9. Drag a precedence constraint from the For Loop Container to the Sequence Container and
rearrange the control flow if necessary.
10. Run the package, and note that the For Loop starts Notepad three times, opening the text file with
the counter variable value in its name (Demo1.txt, Demo2.txt, and Demo3.txt). Close Notepad each
time it opens, and when the execution is complete, stop debugging.
11. Close SQL Server Data Tools, saving the solution files if prompted.
File – You can use this enumerator to iterate through files in a folder.
From Variable – You can use this enumerator to iterate through elements in a variable that contains
an array.
Item – You can use this enumerator to iterate through a property collection for an SSIS object.
Nodelist – You can use this enumerator to iterate through elements and attributes in an XML
document.
SMO – You can use this enumerator to iterate through a collection of SQL Server Management
Objects.
1. Drag the Foreach Loop Container icon from the SSIS Toolbox to the control flow surface.
2. Double-click the Foreach Loop container and select the enumerator you want to use. Each
enumerator has specific properties you need to set, for example the File enumerator requires the path
to the folder containing the files you want to iterate through.
3. Specify the variable in which you want to store the enumerated collection value during each iteration.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-19
4. Drag the tasks you want to perform during each iteration into the Foreach Loop container and
configure their properties appropriately to reference the collection value variable.
Demonstration Steps
Use a Foreach Loop Container
2. Start SQL Server Data Tools and open the ForeachLoopContainer.sln solution in the
D:\Demofiles\Mod07 folder.
5. On the Collection tab, in the Enumerator list, click Foreach File Enumerator. In the Expressions
box, click the ellipsis (…) button. In the Property Expressions Editor dialog box, in the Property list,
click Directory and in the Expression box click the ellipsis (…) button.
6. In the Expression Builder dialog box, expand the Variables and Parameters folder and drag the
$Project::folderPath parameter to the Expression box to specify that the loop should iterate
through files in the folder referenced by the folderPath project parameter. Click OK to close the
Expression Builder, and then in the Property Expressions Editor dialog box, click OK.
7. In the Foreach Loop Editor dialog box, on the Collection tab, in the Retrieve file name section,
select Name and extension to return the file name and extension for each file the loop finds in the
folder.
8. In the Foreach Loop Editor dialog box, on the Variable Mappings tab, in the Variable list, click
User::fName and in the Index column, select 0 to assign the file name of each file found in the
folder to the fName variable. Click OK.
9. Remove the precedence constraints that are connected to and from the Copy File task, and then
drag the Copy File task into the Foreach Loop Container.
10. Create a precedence constraint from the Create Folder task to the Foreach Loop Container, and a
precedence constraint from the Foreach Loop Container to the Send Failure Notification task.
11. Right-click the constraint between the Foreach Loop Container and the Send Failure Notification
task, and click Failure.
12. Run the package, closing each instance of Notepad as it opens. When the package execution has
completed, stop debugging and close SQL Server Data Tools, saving the solution files if prompted.
13. Verify that the D:\Demofiles\Mod07\Demo folder contains each of the files in the
D:\Demofiles\Mod07\Files folder.
MCT USE ONLY. STUDENT USE PROHIBITED
7-20 Implementing Control Flow in an SSIS Package
Objectives
After completing this lab, you will be able to:
Password: Pa$$w0rd
2. Open the Extract Reseller Data.dtsx package and examine its control flow. Note that it contains two
Send Mail tasks—one that runs when either the Extract Resellers or Extract Reseller Sales tasks fail,
and one that runs when the Extract Reseller Sales task succeeds.
3. Examine the settings for the precedence constraint connecting the Extract Resellers task to the Send
Failure Notification task to determine the conditions under which this task will be executed.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-21
4. Examine the settings for the Send Mail tasks, noting that they both use the Local SMTP Server
connection manager.
6. Run the package, and observe the control flow as the task executes.
7. When package execution is complete, stop debugging, and verify that the email message has been
delivered to the C:\inetpub\mailroot\Drop folder. You can double-click the email message to open it
in Outlook.
2. Add a Send Mail task to the control flow, configure it with the following settings, and create a
precedence constraint that runs this task if the Extract Internet Sales task succeeds:
o From: [email protected]
o To: [email protected]
o Priority: Normal
3. Add a second Send Mail task to the control flow, configure it with the following settings, and create
a precedence constraint that runs this task if either the Extract Customers or Extract Internet Sales
task fails:
o From: [email protected]
o To: [email protected]
o Subject: Data Extraction Notification
o Priority: High
2. When package execution is complete, stop debugging, and verify that the failure notification email
message has been delivered to the C:\inetpub\mailroot\Drop folder. You can double-click the email
message to open it in Outlook.
3. Set the ForceExecutionResult property of the Extract Customers task to None. Then run the
package and observe the control flow.
MCT USE ONLY. STUDENT USE PROHIBITED
7-22 Implementing Control Flow in an SSIS Package
4. When package execution is complete, stop debugging, and verify that the success notification email
message has been delivered to the C:\inetpub\mailroot\Drop folder.
5. Close SQL Server Data Tools when you have completed the exercise.
Results: After this exercise, you should have a control flow that sends an email message if the Extract
Internet Sales task succeeds, or sends an email message if either the Extract Customers or Extract
Internet Sales tasks fail.
2. Create a Variable
3. Create a Parameter
3. Open the Extract Payment Data.dtsx package and examine its control flow. Note that it contains a
single Data Flow task named Extract Payments.
4. View the Extract Payments data flow and note that it contains a flat file source named Payments
File, and an OLE DB destination named Staging DB.
5. View the settings of the Payments File source and note that it uses a connection manager named
Payments File.
6. In the Connection Managers pane, double-click Payments File, and note that it references the
Payments.csv file in the D:\Labfiles\Lab07A\Starter\Ex2 folder. This file has the same data structure as
the payments file in the D:\ Accounts folder.
o Name: fName
Note that the value property includes a space on either side of the “-“ character.
o Name: AccountsFolderPath
o Value: D:\Accounts\
o Sensitive: False
o Required: True
@[$Project::AccountsFolderPath]+ @[User::fName]
2. Run the package and view the execution results to verify that the data in the D:\Accounts\Payments -
US.csv file was loaded.
3. Close SQL Server Data Tools when you have completed the exercise.
Results: After this exercise, you should have a package that loads data from a text file based on a
parameter that specifies the folder path where the file is stored, and a variable that specifies the file name.
You have also successfully created a package that loads payments data from a single CSV file, based on a
dynamically-derived folder path and file name. Now you must extend this solution to iterate through all
the files in the folder and import data from each one.
2. Open the Extract Internet Sales Data.dtsx package and modify its control flow so that:
o The Extract Customers and Extract Internet Sales tasks are contained in a Sequence container
named Extract Customer Sales Data.
o The Send Failure Notification task is executed if the Extract Customer Sales Data container
fails.
o The Send Success Notification task is executed if the Extract Customer Sales Data container
succeeds.
3. Run the package to verify that it successfully completes both Data Flow tasks in the sequence, and
then executes the Send Success Notification task.
3. Configure the Foreach Loop Container so that it loops through the files in the folder referenced by
the AccountsFolderPath parameter, adding each file to the fName variable.
4. Run the package and count the number of times the Foreach Loop is executed.
5. When execution has completed, stop debugging and view the results to verify that all files in the
D:\Accounts folder were processed.
6. Close SQL Server Data Tools when you have completed the exercise.
Results: After this exercise, you should have one package that encapsulates two Data Flow tasks in a
Sequence container, and another that uses a Foreach Loop to iterate through the files in a folder specified
in a parameter —and uses a Data Flow task to load their contents into a database.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-25
Lesson 4
Managing Consistency
SSIS solutions are generally used to transfer data from one location to another. Often, the overall SSIS
solution can include multiple data flows and operations; it may be important to ensure that the process
always results in data that is in a consistent state, even if some parts of the process fail.
This lesson discusses techniques for ensuring data consistency when packages fail.
Lesson Objectives
After completing this lesson, you will be able to:
Use transactions.
Use checkpoints.
MaximumErrorCount – This property specifies the maximum number of errors that can occur before
the item fails. The default value for this property is 1.
You can use these properties to achieve fine-grained control of package behavior in the event of an error
that causes a task to fail.
MCT USE ONLY. STUDENT USE PROHIBITED
7-26 Implementing Control Flow in an SSIS Package
Using Transactions
Transactions ensure that all data changes in a
control flow either succeed or fail as a single,
atomic unit of work. When tasks are enlisted in a
transaction, a failure of any single task causes all
tasks to fail, ensuring that the data affected by the
control flow remains in a consistent state, with no
partial data modifications.
Supported – this object will enlist in a transaction if its parent is participating in one.
NotSupported – this object does not support transactions and will not enlist in an existing
transaction.
SSIS transactions rely on the Microsoft Distributed Transaction Coordinator (MSDTC), a system component
that coordinates transactions across multiple data sources. An error will occur if an SSIS package attempts
to start a transaction when the MSDTC service is not running.
SSIS supports multiple concurrent transactions in a single hierarchy of packages, containers, and tasks, but
does not support nested transactions. To understand how multiple transactions behave in a hierarchy,
consider the following facts:
If the child container includes a task with a TransactionOption value of Supported, the task will not
start a new transaction, but will join any transaction started by its parent.
If the child container contains a task with a TransactionOption value of Required, the task will start
a new transaction. However, the new transaction is unrelated to the existing transaction, and the
outcome of one transaction will have no effect on the other.
For more detailed information on transactions in SSIS, see SQL Server Books Online:
Demonstration Steps
Use a Transaction
1. If you did not complete the previous demonstrations in this module, ensure that the 20767A-MIA-DC
and 20767A-MIA-SQL virtual machines are both running, and log on to 20767A-MIA-SQL as
ADVENTUREWORKS\Student with the password Pa$$w0rd. Then, in the D:\Demofiles\Mod07
folder, run Setup.cmd as administrator.
2. Start SQL Server Management Studio and connect to the localhost database engine instance using
Windows authentication.
3. In Object Explorer, expand Databases, expand DemoDW, and then expand Tables.
4. Right-click dbo.StagingTable and click Select Top 1000 Rows to verify that it contains product
data.
5. Right-click dbo.ProductionTable and click Select Top 1000 Rows to verify that it is empty.
6. Start SQL Server Data Tools and open the Transactions.sln solution in the D:\Demofiles\Mod07
folder.
7. In Solution Explorer, double-click Move Products.dtsx. Note that the control flow consists of a Data
Flow task named Copy Products that moves products from a staging table to a production table, and
an SQL Command task named Update Prices that sets the product price.
8. On the Debug menu, click Start Debugging to run the package and note that the Update Prices
task fails. Then on the Debug menu, click Stop Debugging.
9. In SQL Server Management Studio, select the top 1,000 rows from the dbo.ProductionTable table,
noting that it now contains product data but the prices are all set to 0.00. You want to avoid having
products with invalid prices in the production table, so you need to modify the SSIS package to
ensure that, when the price update task fails, the production table remains empty.
10. On the File menu, point to New, and then click Query with Current Connection, type the following
Transact-SQL code, and then click Execute. This deletes all rows in the dbo.ProductionTable table:
11. In SQL Server Data Tools, click anywhere on the Control Flow surface and press F4. Then in the
Properties pane, set the TransactionOption property to Required.
12. Click the Copy Products task, and in the Properties pane, set the FailPackageOnFailure property to
True and ensure the TransactionOption property is set to Supported.
13. Repeat the previous step for the Update Prices task.
14. Run the package and note that the Update Prices task fails again. Then stop debugging.
15. In SQL Server Management Studio, select the top 1,000 rows from the dbo.ProductionTable table,
noting that it is empty, even though the Copy Products task succeeded. The transaction has rolled
back the changes to the production table because the Update Prices task failed.
16. In SQL Server Data Tools, double-click the Update Prices task and change the SQLStatement
property to UPDATE ProductionTable SET Price = 100. Then click OK.
17. Run the package and note that all tasks succeed. Then stop debugging and close SQL Server Data
Tools.
MCT USE ONLY. STUDENT USE PROHIBITED
7-28 Implementing Control Flow in an SSIS Package
18. In SQL Server Management Studio, select the top 1,000 rows from the dbo.ProductionTable table,
noting that it now contains products with valid prices.
Using Checkpoints
Another way you can manage data consistency is to
use checkpoints. You can use checkpoints to restart
a failed package after the issue that caused it to fail
has been resolved. Any tasks that were previously
completed successfully are ignored, and the
execution resumes at the point in the control flow
where the package failed. While checkpoints do not
offer the same level of atomic consistency as a
transaction, they can provide a useful solution when
a control flow includes a long-running or resource-
intensive task that you do not wish to repeat
unnecessarily—such as downloading a large file
from an FTP server.
Checkpoints work by saving information about work in progress to a checkpoint file. When a failed
package is restarted, the checkpoint file is used to identify where to resume execution in the control flow.
To enable a package to use checkpoints, you must set the following properties of the package:
CheckpointFileName –The full file path where you want to save the checkpoint file.
SaveCheckpoints – A Boolean value used to specify whether or not the package should save
checkpoint information to the checkpoint file.
CheckpointUsage – An enumeration with one of the following values:
o Always: The package will always look for a checkpoint file when starting. If none exists, the
package will fail with an error.
o Never: The package will never use a checkpoint file to resume execution and will always begin
execution with the first task in the control flow.
o IfExists: If a checkpoint file exists, the package will use it to resume where it failed previously. If
no checkpoint file exists, the package will begin execution with the first task in the control flow.
Demonstration Steps
Use a Checkpoint
1. If you did not complete the previous demonstrations in this module, ensure that the 20767A-MIA-DC
and 20767A-MIA-SQL virtual machines are both running, and log on to 20767A-MIA-SQL as
ADVENTUREWORKS\Student with the password Pa$$w0rd. Then, in the D:\Demofiles\Mod07
folder, run Setup.cmd as administrator.
2. Start SQL Server Management Studio and connect to the localhost database engine instance using
Windows authentication.
4. Right-click dbo.StagingTable and click Select Top 1000 Rows to verify that it contains product
data.
5. Start Excel and open Products.csv in the D:\Demofiles\Mod07 folder. Note that it contains details for
three more products. Then close Excel.
6. Start SQL Server Data Tools 2015 and open the Checkpoints.sln solution in the
D:\Demofiles\Mod07 folder.
7. In Solution Explorer, double-click Load Data.dtsx. Note that the control flow consists of a File System
task to create a folder, a second File System task to copy the products file to the new folder, and a
Data Flow task that loads the data in the products file into the staging table.
8. Click anywhere on the Control Flow surface to select the package, and press F4. Then in the
Properties pane, set the following properties:
o CheckpointFileName: D:\Demofiles\Mod07\Checkpoint.chk
o CheckpointUsage: IfExists
o SaveCheckpoints: True
9. Set the FailPackageOnFailure property for all three tasks in the control flow to True.
10. On the Debug menu, click Start Debugging to run the package and note that the Load to Staging
Table task fails. Then on the Debug menu click Stop Debugging.
11. In the D:\Demofiles\Mod07 folder, note that a file named Checkpoint.chk has been created, and
that the File System tasks that succeeded have created a folder named Data and copied the
Products.csv file into it.
12. In SQL Server Data Tools, view the Data Flow tab for the Load to Staging Table task, and double-
click the Derive Columns transformation. Change the expression for the NewPrice column to 100,
and click OK.
13. View the Control Flow tab, and then run the package. Note that the Create Folder and Copy File
tasks, which succeeded previously, are not re-executed. Only the Load to Staging Table task is
executed.
14. Stop debugging, and verify that the Checkpoint.chk file is deleted now that the package has been
executed successfully.
15. In SQL Server Management Studio, select the top 1,000 rows from the dbo.StagingTable, and note
that it now contains data about six products.
16. Close SQL Server Management Studio and SQL Server Data Tools.
MCT USE ONLY. STUDENT USE PROHIBITED
7-30 Implementing Control Flow in an SSIS Package
You have developed an SSIS package containing two Execute SQL Task tasks. The first
task extracts data and the second loads a table. A checkpoint is configured for the
package. During execution, the second task fails after loading 50 percent of the table
data. What will happen when the package is next executed (assuming the error which
caused the failure has been rectified)?
The package will rerun the second task, loading the remaining 50 percent of the
table data.
The package will rerun the second task from the beginning.
The package will rerun the first task but skip the second task to avoid another
failure.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-31
Objectives
After completing this lab, you will be able to:
Use transactions.
Use checkpoints.
Password: Pa$$w0rd
4. Implement a Transaction
2. In the Staging database, view the contents of the dbo.Customers and dbo.InternetSales tables to
verify that they are both empty.
2. Open the Extract Internet Sales Data.dtsx package and examine its control flow.
MCT USE ONLY. STUDENT USE PROHIBITED
7-32 Implementing Control Flow in an SSIS Package
3. Run the package, noting that the Extract Customers task succeeds, but the Extract Internet Sales
task fails. When execution is complete, stop debugging.
4. In SQL Server Management Studio, verify that the dbo.InternetSales table is still empty, but the
dbo.Customers table now contains customer records.
5. In SQL Server Management Studio, execute the following Transact-SQL query to reset the staging
tables:
2. Ensure that the Extract Customers and Extract Internet Sales tasks both support transactions, and
configure them so that, if they fail, their parent also fails.
2. In SQL Server Management Studio, verify that both the dbo.InternetSales and dbo.Customers
tables are empty.
3. View the data flow for the Extract Internet Sales task, and modify the expression in the Calculate
Sales Amount derived column transformation to remove the text “/ (OrderQuantity %
OrderQuantity)”. The completed expression should match the following code sample:
UnitPrice * OrderQuantity
4. Run the Extract Internet Sales Data.dtsx package, noting that the Extract Customers and Extract
Internet Sales tasks both succeed. When execution is complete, stop debugging.
5. In SQL Server Management Studio, verify that both the dbo.InternetSales and dbo.Customers
tables contain data.
6. Close SQL Server Data Tools when you have completed the exercise.
Results: After this exercise, you should have a package that uses a transaction to ensure that all Data Flow
tasks succeed or fail as an atomic unit of work.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-33
3. Implement Checkpoints
2. Open the Extract Reseller Data.dtsx package and examine its control flow.
3. Run the package, noting that the Extract Resellers task succeeds, but the Extract Reseller Sales task
fails. When execution is complete, stop debugging.
4. In SQL Server Management Studio, verify that the dbo.ResellerSales table is still empty, but the
dbo.Resellers table now contains reseller records.
5. In SQL Server Management Studio, execute the following Transact-SQL query to reset the staging
tables:
o CheckpointFileName: D:\ETL\CheckPoint.chk
o CheckpointUsage: IfExists
o SaveCheckpoints: True
2. Configure the properties of the Extract Resellers and Extract Reseller Sales tasks so that, if they fail,
the package also fails.
2. Run the Extract Reseller Data.dtsx package, noting once again that the Extract Resellers task
succeeds, but the Extract Reseller Sales task fails. When execution is complete, stop debugging.
3. View the contents of the D:\ETL folder and verify that a file named CheckPoint.chk has been created.
4. In SQL Server Management Studio, verify that the dbo.ResellerSales table is still empty, but the
dbo.Resellers table now contains reseller records.
MCT USE ONLY. STUDENT USE PROHIBITED
7-34 Implementing Control Flow in an SSIS Package
5. View the data flow for the Extract Reseller Sales task, and modify the expression in the Calculate
Sales Amount derived column transformation to remove the text “/ (OrderQuantity %
OrderQuantity)”. The completed expression should match the following code sample:
UnitPrice * OrderQuantity
6. Run the Extract Reseller Sales Data.dtsx package, noting the Extract Resellers task is not re-
executed, and package execution starts with the Extract Reseller Sales task, which failed on the last
attempt. When execution is complete, stop debugging.
7. In SQL Server Management Studio, verify that the dbo.ResellerSales table now contains data.
8. Close SQL Server Data Tools when you have completed the exercise.
Results: After this exercise, you should have a package that uses checkpoints, so that execution can be
restarted at the point of failure of the previous execution.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-35
Review Question(s)
Question: You have an existing SSIS package containing three tasks. You want Task 3 to run
if Task 1 or Task 2 fails. How can you accomplish this?
Question: Which container should you use to perform the same task once for each file in a
folder?
Question: Your package includes an FTP task that downloads a large file from an FTP folder
and a Data Flow task that inserts data from the file into a database. The Data Flow task may
fail if the database is unavailable, in which case you plan to run the package again, after
bringing the database online. How can you avoid downloading the file again when the
package is re-executed?
MCT USE ONLY. STUDENT USE PROHIBITED