Tải bản đầy đủ (.pdf) (10 trang)

Hands-On Microsoft SQL Server 2008 Integration Services part 13 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (480.87 KB, 10 trang )

98 Hands-On Microsoft SQL Server 2008 Integration Services
System task and will see that the variable value is used by the task to create the required
folder.
10. Delete the newly created folder FSTdirectory form the C:\SSIS folder and the
connection manager from BIDS by simply selecting it and pressing . Click
Yes on the pop-up dialog box to confirm deletion of the connection manager.
11. Right-click anywhere on the blank surface of the Control Flow Panel and choose
Variables from the context menu. This will open the Variables window. Add a
variable by clicking the Add Variable button, which is the leftmost button on
the menu bar in the Variables window. In the Name column, type directory
and change the Data Type to String. In the Value field, type C:\ SSIS\
FSTdirectory, as shown in the Figure 3-8.
12. Double-click the File System task to open the Task Editor. In the
IsSourcePathVariable field’s drop-down list, select True. Note that this
converts the SourceConnection field to a SourceVariable field.
13. Click in the SourceVariable field and select the User::directory variable from
the list of variables. Click OK to close the File System Task Editor as shown in
Figure 3-9.
14. Press 5 to execute the package. The File System task turns green and the Output
window shows the Success message.
15. Press -5 to stop debugging the package, or choose Debug | Stop Debugging.
Switch to Windows Explorer to see that the folder has been created.
16. In BIDS, choose File | Save All and leave the project open, as you will return to
this project later in the chapter.
Review
You have created a user-defined variable and used its value to populate the
SourceVariable field in the File System Task Editor. Because you specified the
IsSourcePathVariable as True, so when you run this package, the File System task
knows that it has to read the value of the SourceVariable field from the User::directory
variable. Though you have defined a static value to the variable, this is a good example
of how a variable can affect the task. In real-life scenarios, the variables are populated


either by package configurations or by the upstream tasks in the package at run time
and the downstream tasks use those variables to get dynamically updated values.
Figure 3-8 Adding a variable to the package
Chapter 3: Nuts and Bolts of the SSIS Workflow 99
Precedence Constraints
While creating the workflow and linking tasks, you can decide the conditions that
can affect the running of successive tasks. To do this, you use precedence constraints
when linking two or more executables. Within the SSIS world, an executable is defined
as a container, a task, or an event handler. The executable for which you define the
precedence constraint to be applied after it has run is called the precedence executable,
and the executable for which you define a precedence constraint to be applied before it
could run is called the constrained executable. The precedence constraints can be affected
by the execution result of the precedence executable. For example, you may wish to stop
Figure 3-9 Configuring the File System Task Editor to get a value from a user-defined variable
100 Hands-On Microsoft SQL Server 2008 Integration Services
the constrained executable from running if the precedence executable fails to complete.
The precedence constraints created in a package can also be seen in the Package
Explorer tab in BIDS.
When you connect two tasks on the Control Flow Panel by dragging the green
arrow from a task on to the other task, you actually use a precedence constraint. You
can open the Precedence Constraint Editor by double-clicking the green and can
specify various options for configuring the constraints. The Editor window is divided
into two parts: Constraint Options and Multiple Constraints.
Constraint Options
The precedence constraint can be based on a combination of the execution results and
the evaluation of the expressions. In the Constraint Options section, you can select the
execution result and provide an expression to be evaluated.
Evaluation Operation
c is defines the EvalOp property of the constraint. You
have four choices here that you can see when you click the down arrow button.

e selection of the option here determines the way the precedence constraint
combines execution results and evaluation expressions to determine whether to
run the constrained executable.
Constraint
c Use only the execution result from the precedence executable to
determine whether the constrained executable runs.
Expression
c Use only the Expression property to determine whether the
constrained executable runs. When selected, the Expression field becomes
available to enable you to define an expression, which must be a valid SSIS
expression.
Expression And Constraint c Combines the requirements of both the
Constraint and the Expression.
Expression Or Constraint
c Uses either of the two options to decide the
execution of the constrained executable.
Value
c is field refers to the value property of the precedence constraint and
specifies the value of the execution result of the precedence executable to determine
whether to run the constrained executable. You have three options available here:
Completion
c e constrained executable will run on the completion of the
precedence executable with no regard to the outcome—that is, success or failure.
For example, you may want to run a consistency check on your database toward
the end of your nightly update operation without regard to whether the data
uploading task successfully loaded data or failed to load any data.
Chapter 3: Nuts and Bolts of the SSIS Workflow 101
Success c e constrained executable will run only when the precedence
executable has completed successfully or has a result of “No Errors Occurred”
from the preceding task. e No Errors Occurred result is returned by a

disabled task in the control flow to let the work flow continue.
Failure c e constrained executable will run only when the precedence
executable has failed. For example, you may want to alter the data flow when
the data loading task fails.
Expression
c In this field, you can specify an expression that evaluates to
Boolean true to run the constrained executable. is Expression field becomes
available only when you choose the Expression and Constraint or Expression
Or Constraint option in the Evaluation Operation field. e expression can
include functions, operators, and system and custom variables. You can click Test
to evaluate the expression or test whether the expression you have specified is
syntactically correct. A simple expression is shown in Figure 3-10.
Figure 3-10 The Precedence Constraint Editor
102 Hands-On Microsoft SQL Server 2008 Integration Services
Multiple Constraints
A constrained executable can have multiple constraints—that is, more than one
precedence executable may apply precedence constraints on a constrained executable.
In situations for which you have multiple constraints on a constrained executable,
you can specify the criteria for multiple constraints to control the execution of the
constrained executable in the Multiple Constraints section. You can choose one of the
two mutually exclusive options—Logical AND or Logical OR. Selecting Logical AND
sets the LogicalAnd property of the precedence constraints to True and implies that
all the constraints must evaluate to True before running the constrained executable.
Selecting Logical OR sets the LogicalAnd property to False and implies that one of the
constraints must evaluate to True to run the constrained executable.
The precedence constraint behavior is covered in a Hands-On exercise in Chapter 5,
where you will be able to see how the constrained executables are affected by your
choice of a particular constraint.
Integration Services Expressions
Integration Services allows you to set task values dynamically using variables that are

updated at run time by other tasks. This facility is quite powerful and great in many
respects; however, it does not solve all the real-life problems associated with population
of configuration values. Developers must often evaluate a value for a task based on a
value generated by another task. This requires that they write a custom piece of code to
evaluate the required value, as the value generated by the first task can’t be directly used
in the second task. For example, if you want to create a folder in which the update files
you receive daily are kept, and you want to add a date part in the name of the folder,
you must evaluate the folder name using the two values: one for the static part and
one for the dynamic date part. This is where Integration Services shows its real power.
SSIS allows you to evaluate a value using expressions. SSIS tasks expose some of their
properties that can be dynamically updated using expressions. Expressions can be used
for the following purposes:
Variables and property expressions can use expressions to update values at run time.
c
Integration Services has a For Loop Container, which you will study in the next c
chapter that uses expressions for the looping criteria.
As you’ve seen earlier in the chapter, precedence constraints use expressions to
c
evaluate a condition.
Data flow transformations such as the Conditional Split Transformation and the
c
Derived Column Transformation have an Expression Builder interface.
Chapter 3: Nuts and Bolts of the SSIS Workflow 103
Integration Services expressions consist of one or more variables, columns, functions,
and operators. You can use all these together to build an expression to evaluate the
required value. The language of the Integration Services expressions uses syntax similar
to that used in C and C#. The Expression syntax contains identifiers, literals, operators,
and functions. The collection of syntax, data type conversion rules, truncation rules,
and string padding is called the Expression grammar.
SSIS provides GUI tools to help you build expressions. The Expression Builder is

present in most of the Control Flow tasks in the Expressions page to help you apply
expressions to properties of the tasks. The expressions you apply to properties are called
Property Expressions. Figure 3-11 shows the example of property expression for the
File System task that you used in the last Hands-On exercise. If you open the package
again and open Expression Builder via the Expressions page and build the expression
shown (as explained in the next paragraph), you can then click Evaluate Expression to
evaluate this expression at design time.
In the Expression Builder, you can type information into the Expression box and
drag and drop any of the items from the top two panes to the Expression box to build
Figure 3-11 Expression Builder user interface
104 Hands-On Microsoft SQL Server 2008 Integration Services
your expression. In the top-left pane of this dialog box, you can drag and drop any
of the system- or user-defined variables. When you drop variables in the Expression
box, the required syntax elements such as the @ prefix are automatically added to the
variable names. You can also select columns from this pane if they are applicable to
the task and are available there. And from the pane on the right, you can choose from
various functions and operators. Figure 3-11 also shows a sample expression, which
was built using literals, concatenate operator (+) from the operators, system- and user-
defined variables, and a type cast to convert the date type to string.
You can evaluate your expression at design time by clicking Evaluate Expression and
the result will be shown in the Evaluated Value field. This will not include any values
that are not available at design time. Understand that some of the variables will be
populated at run time and they will have no value at design time; hence the evaluation
may not give you the exact result that will be created at run time. This evaluation of
an expression is accomplished by the Expression Evaluator, which also determines
whether expressions adhere to the rules of Expression grammar. As mentioned, property
expressions can be applied to any of the exposed properties in Control Flow tasks and
to some of the data flow components, as not all data flow components support property
expressions. The data flow components that support property expressions expose their
properties on the parent data flow task.

You can find functions and operators listed in the top-right pane of the Expression
Builder. Integration Services Expression language has a rich set of syntax rules and
functions that cannot be covered in detail in this book. However, detailed explanations
for the expressions used will be provided wherever applicable. Refer to Microsoft SQL
Server 2008 Books Online for more details on expressions.
Following is the brief discussion of these functions and operators.
Mathematical Functions
c When you expand this folder, you will see 11 functions,
such as ABS, LOG, SQUARE, and SQRT. ese functions perform calculations
based on numeric input values provided as parameters to the functions and return
numeric values.
String Functions c is folder contains 14 string functions, such as LEN, LOWER,
LTRIM, RIGHT, and SUBSTRING. String functions perform specified operations
on string or hexadecimal input values and return a string or numeric value.
Date/Time Functions
c Eight Date/Time functions here, such as DATEADD,
GETDATE, and MONTH, perform operations on date and time values and can
return string, numeric, or date and time values.
NULL Functions
c irty functions, such as ISNULL and NULL, are available
for particular data types. ISNULL returns a Boolean result based on whether an
expression is null, whereas NULL returns a null value of a requested data type.
Chapter 3: Nuts and Bolts of the SSIS Workflow 105
Type Casts c ese twenty-nine functions help you perform data type conversions
for a variable, a column, or an expression from one data type to another, such as
string to integer or date/time to string.
Operators
c About twenty-two operators, such as (+) add, (+) concatenate, (/)
divide, and (>) greater than.
You will be using property expressions in various Hands-On exercises throughout

this book, but just to tickle your brain, the following is a simple exercise using property
expressions.
Hands-On: Using Expressions
to Update Properties at Run Time
You need to create a directory to keep the update files you receive daily. But this time,
you want to keep the daily update files in their own folder—you need to create a folder
each night and want to include a date part in the name of the folder.
Method
You can do this in at least two ways. You can write an expression on the variable’s
value property to evaluate the new value and pass that value to the SourceConnection
field. Or you can go back to the first part of the last Hands-On exercise, in which
you used a connection manager for the SourceConnection field and provided a static
value of C:\SSIS\FSTdirectory. As part of the second method, you can add property
expression on the FSTdirectory Connection Manager to generate a connection string at
run time. You will be using the second method in this exercise so that you can have an
introduction to the Expression Builder.
Exercise (Configuring the File System
Task with Hard-Coded Values)
The first part of this exercise is to open the Creating Directory project and change the
settings to go back to a hard-coded value for the SourceConnection field.
1. Open the Creating Directory project with BIDS if it is not already open.
2. Double-click the File System Task to open the editor.
3. Click in the IsSourcePathVariable field and change the value to False.
4. Click in the SourceConnection field and choose <New connection…> from
the drop-down list. Choose Create Folder in the Usage Type field of the File
Connection Manager Editor. Type SSISdirectory in the Folder field. You don’t
need to provide a path here, as that will be evaluated at run time using the expression
you are going to build. Click OK twice to close the File System Task Editor.
106 Hands-On Microsoft SQL Server 2008 Integration Services
Exercise (Using Property Expressions to Evaluate

ConnectionString at Run Time)
In this part, you will build a property expression on the ConnectionString property of
the FSTdirectory Connection Manager to evaluate the directory name, similar to C:\
SSIS\FSTdirectory20091014, having the date attached as a suffix to the folder name.
5. In the Connection Manager area, right-click the FSTdirectory Connection
Manager and choose Properties from the context menu.
6. In the Properties window, click in the Expressions field and then click the ellipsis
button. This will open the Property Expressions Editor.
7. Click in the Property column and choose ConnectionString Property from
the drop-down list. Click the ellipsis button shown on the right to open the
Expression Builder.
8. In the top left pane of Expression Builder, expand Variables and drag and drop
the User::directory variable in the Expression area. Then do the following in
sequence:
c Expand Operators and add the concatenate (+) operator.
c Expand Type Casts and add (DT_WSTR, <<length>>); then change <<length>>
to 4 to make it (DT_WSTR, 4).
c Expand Date/Time Functions and add YEAR( <<date>> ); then drop the
GETDATE() function on the <<date>> part of the YEAR( <<date>> )
function.
Your expression should look like this:
@[User::directory] + (DT_WSTR, 4) YEAR( GETDATE() )
9. Click Evaluate Expression, and you will see a value of current year attached to
the User::directory variable (which in my case is C:\ SSIS\FSTdirectory2009 at
this point in time). Complete the expression by adding the following so that the
complete expression looks like one shown in Figure 3-12:
RIGHT("0" + (DT_WSTR, 2) MONTH( GETDATE() ), 2) +
RIGHT("0" + (DT_WSTR, 2) DAY( GETDATE() ), 2)
10. Click OK twice to return to the Properties window. If you expand the Expressions
field, you should see the expression you developed earlier.

11. Press 5 on the keyboard to execute the package. When the File System task has
turned green, press -
5 to stop debugging and go back to design mode.
Choose File | Save All and then close the project.
12. Using Windows Explorer, check to see that the required folder has been created.
Chapter 3: Nuts and Bolts of the SSIS Workflow 107
Review
You’ve learned one of the core skills that will help you create packages that can update
themselves using the current values at run time. You’ve used Expression Builder to
write an expression. You understand that this can also be written directly without using
Expression Builder’s drag and drop feature to speed things up, but Expression Builder
helps in providing quick syntax help.
Summary
This chapter covered the basic components used for building a workflow in an SSIS
package. Integration Services provides connection managers that help various tasks in
the package to establish connections to the external data stores. Variables provided in
SSIS are different from those in DTS 2000; they can have scope and are categorized
as system variables that are read-only which you cannot create and modify, or as the
user-defined variables that you create to meet the needs of the package. Precedence
Figure 3-12 Building a property expression

×