0% found this document useful (0 votes)
61 views36 pages

20767A - Implementing A SQL Data Warehouse-195-230

Uploaded by

Ocean World
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views36 pages

20767A - Implementing A SQL Data Warehouse-195-230

Uploaded by

Ocean World
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

MCT USE ONLY.

STUDENT USE PROHIBITED


7-1

Module 7
Implementing Control Flow in an SSIS Package
Contents:
Module Overview 7-1

Lesson 1: Introduction to Control Flow 7-2

Lesson 2: Creating Dynamic Packages 7-10

Lesson 3: Using Containers 7-15

Lab A: Implementing Control Flow in an SSIS Package 7-20

Lesson 4: Managing Consistency 7-25

Lab B: Using Transactions and Checkpoints 7-31


Module Review and Takeaways 7-35

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.

 Create dynamic packages that include variables and parameters.

 Use containers in a package control flow.

 Enforce consistency with transactions and checkpoints.


MCT USE ONLY. STUDENT USE PROHIBITED
7-2 Implementing Control Flow in an SSIS Package

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:

 Describe the control flow tasks provided by SSIS.

 Define a workflow for tasks by using precedence constraints.

 Use design time features of SSIS to help you develop control flow efficiently.

 Use multiple packages in an SSIS solution.


 Create reusable package templates.

Control Flow Tasks


A control flow consists of one or more tasks. SSIS
includes the following control flow tasks that you
can use in a package:

Data Flow Tasks

Data Flow Encapsulates a data flow that transfers data from a source to a
destination.

Database Tasks

Data Profiling Generates statistical reports based on a data source.

Bulk Insert Inserts data into a data destination in a bulk load operation.

Execute SQL Runs a structured query language (SQL) query in a database.

Execute T-SQL Runs a Transact-SQL query in a Microsoft SQL Server database.


Statement

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

Data Flow Tasks

File and Internet Tasks

File System Performs file system operations, such as creating folders or deleting
files.

FTP Performs file transfer protocol (FTP) operations, such as copying files.

XML Performs XML processing operations, such as applying a style sheet.

Web Service Calls a method on a specific web service.

Send Mail Sends an email message.

Message Queue Sends and receives MSMQ messages between an SSIS package and an
Application Queue.

Process Execution Tasks

Execute Package Runs a specified SSIS package.

Execute Process Runs a specified app.

WMI Tasks

WMI Data Reader Runs a Windows Management Instrumentation (WMI) query.

WMI Event Watcher Monitors a specific WMI event.

Custom Logic Tasks

Script Runs Microsoft Visual Studio® Tools for Applications (VSTA) script
written in Microsoft Visual Basic or Microsoft Visual C#.

Custom Task A custom task implemented as a .NET assembly.

Database Transfer Tasks

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 Tasks

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

Data Flow Tasks


Execute DDL instance—for example, to create a cube.

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.

Big Data Tasks

Hadoop File System Runs a Hadoop File System task.

Hadoop Hive Runs a Hive Script on a Hadoop Cluster.

Hadoop Pig Runs a Pig Script on a Hadoop Cluster.

SQL Server Maintenance Tasks

Backup Database Backs up an SQL Server database.

Check Database Checks the integrity of an SQL Server database.


Integrity

History Cleanup Deletes out-of-date history data for SQL Server maintenance
operations.

Maintenance Cleanup Deletes files left by maintenance operations.

Notify Operator Sends a notification by email message, pager message, or network alert
to an SQL Agent operator.

Rebuild Index Rebuilds a specified index on an SQL Server table or view.

Reorganize Index Reorganizes a specified index on an SQL Server table or view.

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.

Control Flow Conditions


You can define precedence constraints for one of
the following three conditions:

 Success – The execution flow to follow when a


task completes successfully. In the control flow
designer, success constraints are shown as
green arrows.
 Failure – The execution flow to follow when a task fails. In the control flow designer, failure
constraints are shown as red arrows.

 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.

Using Multiple Constraints

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 and Annotations


As your control flows become more complex, it can
be difficult to interpret the control flow surface. The
SSIS Designer includes two features to help SSIS
developers work more efficiently.

Grouping Tasks

You can group multiple tasks on the design surface


to manage them as a single unit. A task grouping is
a “design time only” feature and has no effect on
run-time behavior. With a grouped set of tasks, you
can:

 Move the tasks around the design surface as a


single unit.

 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: Implementing Control Flow


In this demonstration, you will see how to:
 Add tasks to a control flow.

 Use precedence constraints to define a control flow.

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.

2. In the D:\Demofiles\Mod07 folder, run Setup.cmd as Administrator.

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 Name: Delete Files

o Operation: Delete directory content

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 Name: Delete Folder


o Operation: Delete directory

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 Name: Create Folder

o Operation: Create directory


o UseDirectoryIfExists: True

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 Name: Copy File

o Operation: Copy file


o DestinationConnection: Demo

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 Name (on the General tab): Send Failure Notification

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

o From (on the Mail tab): [email protected]

o To (on the Mail tab): [email protected]

o Subject (on the Mail tab): Control Flow Failure

o MessageSource (on the Mail tab): A task failed

Use Precedence Constraints to Define a Control Flow

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.

Using Multiple Packages


Although you can implement an SSIS solution that
includes only one package, most enterprise
solutions include multiple packages. By dividing
your solution into multiple packages, you can:

 Create reusable units of workflow that can be


used multiple times in a single ETL process.

 Run multiple control flows in parallel, taking


advantage of multi-processing computers and
improving the overall throughput of your ETL
processes.

 Separate data extraction workflows to suit data


acquisition windows.
You can execute each package independently, in addition to using the Execute Package task to run one
package from another.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-9

Create a Package Template


SSIS developers often need to create multiple
similar packages. To make the development process
more efficient, you can adopt the following
procedure to create a package template—this can
be reused to create multiple packages with pre-
defined objects and settings:

1. Create a package that includes the elements


you want to reuse. These elements can include:

o Connection Managers
o Tasks

o Event Handlers

o Parameters and Variables

Save the package to the DataTransformationItems folder on your development workstation. By


default, this folder is located at C:\Program Files (x86)\Microsoft Visual Studio
<version>\Common7\IDE\PrivateAssemblies\ProjectItems\DataTransformationProject.
2. When you want to reuse the package, add a new item to the project and select the package in the
Add New Item dialog box.

3. Change the Name and ID properties of the new package to avoid naming conflicts.

Check Your Knowledge


Question

Which of the following is one of the key advantages of developing multiple packages
within an SSIS solution?

Select the correct answer.

It isolates individual units of workflow in the solution.

It creates reusable units of workflow which you can use multiple times in an ETL
process.

Multiple packages result in a less cluttered workspace.

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:

 Create variables in an SSIS solution.

 Create parameters in an SSIS solution.

 Use expressions in an SSIS solution.

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:

 Set property values for tasks and other objects.

 Store an iterator or enumerator value for a


loop.
 Set input and output parameters for an SQL
query.

 Store results from an SQL query.


 Implement conditional logic in an expression.

SSIS packages can contain user and system variables.

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.

 Value – The initial value of the variable.


MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-11

 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:

 MachineName – The computer on which the package is running.

 PackageName – The name of the package that is running.

 StartTime – The time that the package started running.

 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.

Parameters have three kinds of value:

 Design default value – A default value


assigned to the parameter in the design
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.

SSIS supports two kinds of parameter:

 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.

 Conditional split transformation criteria.


 Derived column transformation values.

 Precedence constraint conditions.

Expressions are based on SSIS expression syntax,


which uses similar functions and keywords to
common programming languages like Microsoft
C#. Expressions can include variables and parameters, meaning you can set values dynamically, based on
specific run-time conditions.

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

Demonstration: Using Variables and Parameters


In this demonstration, you will see how to:

 Create a variable.

 Create a parameter.
 Use variables and parameters in an expression.

Demonstration Steps
Create a Variable

1. Ensure you have completed the previous demonstration in this module.

2. Start SQL Server Data Tools and open the VariablesAndParameters.sln solution in the
D:\Demofiles\Mod07 folder.

3. In Solution Explorer, double-click Control Flow.dtsx.

4. On the View menu, point to Other Windows, and click Variables.

5. In the Variables pane, click Add Variable and add a variable with the following properties:

o Name: fName

o Scope: Control Flow

o Data type: String


o Value: Demo1.txt

Create a Parameter

1. In Solution Explorer, double-click Project.params.

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

o Description: Folder containing text files

Note: Be sure to include the trailing “\” in the Value property.

3. Save all files and close the Project.params [Design] window.

Use a Variable and a Parameter in an Expression

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:

 Describe the types of container available in an SSIS package.

 Use a Sequence container to group related tasks.

 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:

 Task containers – Each control flow task has its


own implicit container.

 Sequence containers – You can group tasks


and other containers into a Sequence
container. This creates an execution hierarchy
and means you can set properties at the
container level that apply to all elements within
the container.

 For Loop containers – You can use a For Loop


container to perform an iterative process until a specified condition is met. For example, you could
use a For Loop container to execute the same task a specific number of times.

 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:

 Manage properties for multiple tasks as a unit.

 Disable a logical subset of the package for


debugging purposes.

 Create a scope for variables.

 Manage transactions at a granular level.

To create a Sequence Container, drag the Sequence


Container icon from the SSIS Toolbox pane to the design surface. Then drag the tasks and other
containers you want to include into the Sequence container.

Demonstration: Using a Sequence Container


In this demonstration, you will see how to use a Sequence container.

Demonstration Steps
Use a Sequence Container

1. Ensure you have completed the previous demonstrations in this module.


2. Start SQL Server Data Tools and open the SequenceContainer.sln solution in the
D:\Demofiles\Mod07 folder.

3. In Solution Explorer, double-click Control Flow.dtsx.

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

For Loop Containers


You can use a For Loop container to repeat a
portion of the control flow until a specific condition
is met. For example, you could run a task a specified
number of times.

Conceptually, a For Loop container behaves


similarly to a “for construct”, in common
programming languages such as Microsoft C#. A
For Loop container uses the following expression-
based properties to determine the number of
iterations it performs:

 An optional initialization expression, which sets


a counter variable to an initial value.

 An evaluation expression that typically evaluates a counter variable, to exit the loop when it matches
a specific value.

 An iteration expression that typically modifies the value of a counter variable.

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: Using a For Loop Container


In this demonstration, you will see how to use a For Loop container.

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.

3. In Solution Explorer, double-click Control Flow.dtsx.

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 Scope: Control Flow

o Data type: Int32

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

o EvalExpression: @counter < 4

o AssignExpression: @counter = @counter + 1


MCT USE ONLY. STUDENT USE PROHIBITED
7-18 Implementing Control Flow in an SSIS Package

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 Name (on the General tab): Open File

o Executable (on the Process tab): Notepad.exe

o Expressions (on the Expressions tab): Use the Property Expressions Editor to set the following
expression for the Arguments property:

@[$Project::folderPath] + "Demo" + (DT_WSTR,1)@[User::counter] + ".txt"

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.

Foreach Loop Containers


You can use a Foreach Loop container to perform
an iterative process on each item in an enumerated
collection. SSIS supports various Foreach Loop
enumerators including:

 ADO – You can use this enumerator to loop


through elements of an ADO object; for
example, records in a Recordset.

 ADO.NET Schema Rowset – You can use this


enumerator to iterate through objects in an
ADO.NET schema; for example, tables in a
dataset or rows in a table.

 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.

To use a Foreach Loop container in a control flow:

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: Using a Foreach Loop Container


In this demonstration, you will see how to use a Foreach Loop container.

Demonstration Steps
Use a Foreach Loop Container

1. Ensure you have completed the previous demonstrations in this module.

2. Start SQL Server Data Tools and open the ForeachLoopContainer.sln solution in the
D:\Demofiles\Mod07 folder.

3. In Solution Explorer, double-click Control Flow.dtsx.


4. From the SSIS Toolbox, drag a Foreach Loop Container to the control flow design surface. Then
double-click the Foreach loop Container to view the Foreach Loop Editor dialog box.

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

Lab A: Implementing Control Flow in an SSIS Package


Scenario
You are implementing an ETL solution for Adventure Works Cycles and must ensure that the data flows
you have already defined are executed as a workflow that notifies operators of success or failure by
sending an email message. You must also implement an ETL solution that transfers data from text files
generated by the company’s financial accounting package to the data warehouse.

Objectives
After completing this lab, you will be able to:

 Use tasks and precedence constraints.

 Use variables and parameters.


 Use containers.

Estimated Time: 60 minutes

Virtual machine: 20767A-MIA-SQL

User name: ADVENTUREWORKS\Student

Password: Pa$$w0rd

Exercise 1: Using Tasks and Precedence in a Control Flow


Scenario
You have implemented data flows to extract data and load it into a staging database as part of the ETL
process for your data warehousing solution. Now you want to coordinate these data flows by
implementing a control flow that notifies an operator of the outcome of the process.

The main tasks for this exercise are as follows:

1. Prepare the Lab Environment

2. View the Control Flow

3. Add Tasks to a Control Flow


4. Test the Control Flow

 Task 1: Prepare the Lab Environment


1. Ensure the 20767A-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.

2. Run Setup.cmd in the D:\Labfiles\Lab07A\Starter folder as Administrator.

 Task 2: View the Control Flow


1. Use SQL Server Data Tools to open the AdventureWorksETL.sln solution in the
D:\Labfiles\Lab07A\Starter\Ex1 folder.

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.

5. Examine the settings of 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.

 Task 3: Add Tasks to a Control Flow


1. Open the Extract Internet Sales Data.dtsx package and examine its control flow.

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 Name: Send Success Notification


o SmtpConnection: A new SMTP Connection Manager named Local SMTP Server that connects
to the localhost SMTP server

o From: [email protected]
o To: [email protected]

o Subject: Data Extraction Notification

o MessageSourceType: Direct Input

o MessageSource: The Internet Sales data was successfully extracted

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 Name: Send Failure Notification


o SmtpConnection: The Local SMTP Server connection manager you created previously

o From: [email protected]

o To: [email protected]
o Subject: Data Extraction Notification

o MessageSourceType: Direct Input

o MessageSource: The Internet Sales data extraction process failed

o Priority: High

 Task 4: Test the Control Flow


1. Set the ForceExecutionResult property of the Extract Customers task to Failure. Then run the
package and observe the control flow.

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.

Exercise 2: Using Variables and Parameters


Scenario
You need to enhance your ETL solution to include the staging of payments data that is generated in
comma-separated value (CSV) format from a financial accounts system. You have implemented a simple
data flow that reads data from a CSV file and loads it into the staging database. You must now modify the
package to construct the folder path and file name for the CSV file dynamically at run time instead of
relying on a hard-coded name in the Data Flow task settings.
The main tasks for this exercise are as follows:

1. View the Control Flow

2. Create a Variable

3. Create a Parameter

4. Use a Variable and a Parameter in an Expression

 Task 1: View the Control Flow


1. View the contents of the D:\Accounts folder and note the files it contains. In this exercise, you will
modify an existing package to create a dynamic reference to one of these files.

2. Open the AdventureWorksETL.sln solution in the D:\Labfiles\Lab07A\Starter\Ex2 folder.

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.

7. Run the package, and stop debugging when it has completed.


8. On the Execution Results tab, find the following line in the package execution log:

[Payments File [2]] Information: The processing of the file


“D:\Labfiles\Lab07A\Starter\Ex2\Payments.csv” has started
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-23

 Task 2: Create a Variable


1. Add a variable with the following properties to the package:

o Name: fName

o Scope: Extract Payment Data

o Data type: String

o Value: Payments - US.csv

Note that the value property includes a space on either side of the “-“ character.

 Task 3: Create a Parameter


 Add a project parameter with the following settings:

o Name: AccountsFolderPath

o Data type: String

o Value: D:\Accounts\

o Sensitive: False
o Required: True

o Description: Path to accounts files

Note: Be sure to include the trailing “\” in the Value property.

 Task 4: Use a Variable and a Parameter in an Expression


1. Set the Expressions property of the Payments File connection manager in the Extract Payment
Data package so that the ConnectionString property uses the following expression:

@[$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.

Exercise 3: Using Containers


Scenario
You have created a control flow that loads Internet sales data and sends a notification email message to
indicate whether the process succeeded or failed. You now want to encapsulate the Data Flow tasks for
this control flow in a Sequence container so you can manage them as a single unit.

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.

The main tasks for this exercise are as follows:

1. Add a Sequence Container to a Control Flow

2. Add a Foreach Loop Container to a Control Flow


MCT USE ONLY. STUDENT USE PROHIBITED
7-24 Implementing Control Flow in an SSIS Package

 Task 1: Add a Sequence Container to a Control Flow


1. Open the AdventureWorksETL solution in the D:\Labfiles\Lab07A\Starter\Ex3 folder.

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.

 Task 2: Add a Foreach Loop Container to a Control Flow


1. In the AdventureWorksETL solution, open the Extract Payment Data.dtsx package.
2. Move the existing Extract Payment Data Flow task into a new Foreach Loop Container.

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:

 Configure failure behavior.

 Use transactions.

 Use checkpoints.

Configuring Failure Behavior


An SSIS package control flow can contain nested
hierarchies of containers and tasks. You can use the
following properties to control how a failure in one
element of the control flow determines the overall
package outcome:
 FailPackageOnFailure – When set to True, the
failure of the task or container results in the
failure of the package in which it is defined. The
default value for this property is False.

 FailParentOnFailure – When set to True, the


failure of the task or container results in the
failure of its container. If the item with this
property is not in a container, then its parent is the package, in which case this property has the same
effect as the FailPackageOnFailure property. When setting this property on a package executed by
an Execute Package task in another package, a value of True causes the calling package to fail if this
package fails. The default value for this property is False.

 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.

A task, container, or package’s participation in a


transaction is determined by the
TransactionOption property, which you can set to
one of three possible values:

 Required – this object requires a transaction


and will create a new one if none exists.

 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 a container with a TransactionOption value of Required includes a container with a


TransactionOption of NotSupported, the child container will not participate in the parent
transaction.

 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:

Integration Services Transactions


http://aka.ms/tt04jf
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-27

Demonstration: Using a Transaction


In this demonstration, you will see how to use a transaction.

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:

TRUNCATE TABLE DemoDW.dbo.ProductionTable;

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: Using a Checkpoint


In this demonstration, you will see how to use a checkpoint.

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.

3. In Object Explorer, expand Databases, expand DemoDW, and expand Tables.


MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a SQL Data Warehouse 7-29

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

Check Your Knowledge


Question

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)?

Select the correct answer.

The package will rerun all tasks from the beginning.

The package will fail.

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

Lab B: Using Transactions and Checkpoints


Scenario
You are concerned that, if the Adventure Works ETL data flow fails, it will leave you with a partially-loaded
staging database. To avoid this and ensure data integrity, you intend to use transactions and checkpoints.

Objectives
After completing this lab, you will be able to:

 Use transactions.

 Use checkpoints.

Estimated Time: 30 minutes

Virtual machine: 20767A-MIA-SQL

User name: ADVENTUREWORKS\Student

Password: Pa$$w0rd

Exercise 1: Using Transactions


Scenario
You have created an SSIS package that uses two data flows to extract, transform, and load Internet sales
data. You now want to ensure that package execution always results in a consistent data state, so that if
any of the data flows fail, no data is loaded.
The main tasks for this exercise are as follows:

1. Prepare the Lab Environment

2. View the Data in the Database


3. Run a Package to Extract Data

4. Implement a Transaction

5. Observe Transaction Behavior

 Task 1: Prepare the Lab Environment


1. Ensure 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.

2. Run Setup.cmd in the D:\Labfiles\Lab07B\Starter folder as Administrator.

 Task 2: View the Data in the Database


1. Start SQL Server Management Studio and connect to the localhost database engine instance by
using Windows authentication.

2. In the Staging database, view the contents of the dbo.Customers and dbo.InternetSales tables to
verify that they are both empty.

 Task 3: Run a Package to Extract Data


1. Use SQL Server Data Tools to open the AdventureWorksETL.sln solution in the
D:\Labfiles\Lab07B\Starter\Ex1 folder.

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:

TRUNCATE TABLE Staging.dbo.Customers;

 Task 4: Implement a Transaction


1. Configure the Extract Customer Sales Data Sequence container in the Extract Internet Sales
Data.dtsx package so that it requires a transaction.

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.

 Task 5: Observe Transaction Behavior


1. Run the Extract Internet Sales Data.dtsx package, noting once again that the Extract Customers
task succeeds, but the Extract Internet Sales task fails. Note also that the Extract Customer Sales
Data Sequence container fails. When execution is complete, stop debugging.

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

Exercise 2: Using Checkpoints


Scenario
You have created an SSIS package that uses two data flows to extract, transform, and load reseller sales
data. You now want to ensure that, if any task in the package fails, it can be restarted without re-
executing the tasks that had previously succeeded.

The main tasks for this exercise are as follows:

1. View the Data in the Database

2. Run a Package to Extract Data

3. Implement Checkpoints

4. Observe Checkpoint Behavior

 Task 1: View the Data in the Database


1. Use SQL Server Management Studio to view the contents of the dbo.Resellers and
dbo.ResellerSales tables in the Staging database on the localhost database engine instance.

2. Verify that both of these tables are empty.

 Task 2: Run a Package to Extract Data


1. Open the AdventureWorksETL.sln solution in the D:\Labfiles\Lab07B\Starter\Ex2 folder.

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:

TRUNCATE TABLE Staging.dbo.Resellers;

 Task 3: Implement Checkpoints


1. Set the following properties of the Extract Reseller Data package:

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.

 Task 4: Observe Checkpoint Behavior


1. View the contents of the D:\ETL folder and verify that no file named CheckPoint.chk exists.

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

Module Review and Takeaways


In this module, you have learned how to implement control flow in an SSIS package, and how to use
transactions and checkpoints to ensure data integrity when a package fails.

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

You might also like