Level 6 Stairway to Integration Services

download Level 6 Stairway to Integration Services

of 12

Transcript of Level 6 Stairway to Integration Services

  • 8/13/2019 Level 6 Stairway to Integration Services

    1/12

    Basic SSIS Workflow Management Level 6 of the Stairwayto Integration Services

    By Andy Leonard, 2012/07/18

    The Series

    This article is part of the Stairway Series: Stairway to Integration Services

    Integration Services is one of the most popular subsystems in SQL Server. In allows you to Extract,Transform, and Load (ETL) data between a variety of data sources and programmatically change datain any manner you can think of and script in C#.

    This is the fifth article in a series entitled Stairway to Integration Services. Previous articles in theseries include:

    What is SSIS? Level 1 of the Stairway to Integration Services

    The SSIS Data Pump - Level 2 of the Stairway to Integration Services Adding Rows in Incremental Loads Level 3 of the Stairway to Integration Services

    Updating Rows in Incremental Loads Level 4 of the Stairway to Integration Services Deleting Rows in Incremental Loads Level 5 of the Stairway to Integration Services

    Introduction

    In previous installments, we have focused on loading data using Incremental Loads. In this article, wewill focus on managing workflow in the SQL Server Integration Services Control Flow usingPrecedence Constraints. You will add a new SSIS package to the existing solution as we take a test-and-see approach. Ready? Good. Lets dive in!

    Adding a Package

    The SSIS Control Flow contains Tasks, Containers, and Precedence Constraints. Tasks and

    containers are executable objects in the SSIS Control Flow; Precedence constraints govern when orif tasks or containers will execute.

    To begin working with the Control Flow, open your existing SSIS solution calledMy_First_SSIS_Project. Once open, right-click the SSIS Packages virtual folder in Solution Explorerand click New SSIS Package as shown in Figure 1:

    Figure 1

    When you click New SSIS Package, a new package named Package1.dtsx is created and displayed.To rename this package, right-click Package1.dtsx and click Rename as shown in Figure 2:

  • 8/13/2019 Level 6 Stairway to Integration Services

    2/12

    Figure 2

    The package enters rename mode as shown in Figure 3. Rename the package Precedence.dtsx:

    Figure 3

    After you rename the package and press the Enter key, you will be prompted to rename the packageobject as shown in Figure 4:

  • 8/13/2019 Level 6 Stairway to Integration Services

    3/12

    Figure 4

    Why, Andy? Excellent question! The SSIS packages displayed in Solution Explorer represent the filenamed Package1.dtsx in the Solution\Project folder and the package objectin Business IntelligenceDevelopment Studio (BIDS). When you see the prompt shown in Figure 4, you have already renamedthe file; the prompt is asking if you want to rename the package object in BIDS. Do yourself a favor:Always answer this prompt by clicking the Yes button. If you do not, your file and package object willbe named differently and that is simply confusing.

    Adding a Script Task

    The absence of Precedence constraints is valid, as we will see in our first demo. Lets begin by addinga Script Task to the Control Flow from the Control Flow Toolbox. Click on the Script Task in theToolbox and drag it onto the Control Flow surface as shown in Figure 5:

    Figure 5

    The Script Task is arguably the most powerful SSIS task in SQL Server Integration Services. You can

    use it to create much of the functionality contained in other SSIS tasks. You can also use the ScriptTask to add new functionality to the SSIS Control Flow.

    Right-click the Script Task and click Edit to open the Script task Editor shown in Figure 6:

  • 8/13/2019 Level 6 Stairway to Integration Services

    4/12

    Figure 6

    We will not exhaust Script Task functionality in this article, but we will get started. The first property youwant to examine is the ScriptLanguage property. You can select Visual Basic or C# as yourprogramming language in each Script Task. You can change the default language of the Script taskfrom the BIDS dropdown menus by selecting ToolsOptionsBusiness IntelligenceDesignersIntegration Services Designers. I prefer using Visual Basic as shown in Figure 7:

    Figure 7

  • 8/13/2019 Level 6 Stairway to Integration Services

    5/12

  • 8/13/2019 Level 6 Stairway to Integration Services

    6/12

    Figure 10

    Once you click the OK button, the Script Task ReadOnlyVariables property will appear as shown inFigure 11:

    Figure 11

    For our purposes, this is all we need to configure on this screen at this time. Click the Edit Scriptbutton to open the Visual Studio Tools for Applications script editor as shown in Figure 12:

  • 8/13/2019 Level 6 Stairway to Integration Services

    7/12

    Figure 12

    Scroll until you see Public Sub Main(). Add this code to the subroutine:

    DimsTaskName AsString=

    Dts.Variables("TaskName").Value.ToString

    MsgBox(sTaskName &" completed.")

    Your code should appear as shown in Figure 13:

    Figure 13

  • 8/13/2019 Level 6 Stairway to Integration Services

    8/12

    What does this code accomplish? The first statement creates a variable in Visual Basic (VB). Thevariable is named sTaskName and is created as a String variable. sTaskName is assigned a valuewhen it is created. The value it is assigned is the value of the System::TaskName variable added tothe Script Tasks ReadOnlyVariables list earlier. We reach the SSIS Variable value by accessing theDts namespaces Variables collection. That explains the Dts.Variables part of the assignment. This isfollowed by the name of the SSIS Variable TaskName quoted within parentheses. That explainsthe ("TaskName") part.

    In SSIS, variables have scopeand belong to namespaces. Scope refers to the container towhich the SSIS Variable belongs. Every SSIS Variable is associated with a Namespace. Bydefault, there are two namespaces: System and User. TaskName belongs to the Systemnamespace, denoted by the ::notation. Variable names mustbe unique at a given scope, regardless of namespace.Because there is a TaskName variablein the System namespace, I cannothave a TaskName variable in the User namespace.

    We next obtain the Value property, which is an Object. Because it is an Object, we have to cast theproperty to a String in order to assign it to String variable inside the VB script. We accomplish thecasting via the ToString method.

    This is how we read a value from an SSIS Variable into a Script Task variable (in VB).

    The next line displays a popup message box containing the name of the task from the sTaskName

    variable plus the string " completed.".Close the Script Task Code Editor by clicking the red X in the upper right corner. The script isautomatically saved when you do this. Click the OK button in the Script Task Editor to complete theScript Task configuration.

    We are ready for a test! Click the Play button or press the F5 key to start the SSIS package in thedebugger. You should see a message box similar to Figure 14:

    Figure 14

    Stop the debugger by clicking the VCR-style Stop button.

    Adding a Second Script Task

    Why did we go to all that trouble? Adding the TaskName variable seemed like a lot of extra work for nogood reason. There was a good reason, though. To demonstrate, right-click Script Task 1 and clickCopy. Next, right-click in the whitespace of the Control Flow anywhere except on Script Task 1 and

    click Paste. A new Script Task is added to the Control Flow, named Script Task 1 1 as shown inFigure 15:

  • 8/13/2019 Level 6 Stairway to Integration Services

    9/12

    Figure 15

    Right-click Script Task 1 1 and click Rename. Change the name of the Script task to Script Task 2 asshown in Figure 16:

    Figure 16

    Re-execute the SSIS package in the SSIS debugger. Two message boxes should display (most likelystacked on top of each other) as shown in Figure 17:

  • 8/13/2019 Level 6 Stairway to Integration Services

    10/12

    Figure 17

    Since the name of the Script Task (from the System::TaskName variable) is displayed in the messagebox, we can make lots of Script Tasks by copying and pasting. Cool, huh?

    Stop the debugger.

    Use Cases 0 and 1The preceding sample is an example of a use case. This use case demonstrates what happens ifthere are no Precedence constraints. I call this Use Case 0. For Use Case 1, lets add a Precedenceconstraint between Script Task 1 and Script Task 2. To accomplish this, first click on Script Task 1. APrecedence Constraint appears as a green arrow extending below Script Task 1 as shown in Figure18:

    Figure 18

    Click and drag this Precedence constraint to Script Task 2, then release the mouse button. I see a lotof people try to hit the edge of the next task. Dont. Drop the end of the Precedence constraint in themiddle of the next task as shown in Figure 19:

    Figure 19

    Execute the package in the SSIS debugger. Note Script Task 1 executes first as shown in Figure 20:

  • 8/13/2019 Level 6 Stairway to Integration Services

    11/12

    Figure 20

    Click the OK button to complete Script Task 1. Next, Script Task 2 executes as shown in Figure 21:

    Figure 21

    Note that Script Task 2 executes afterScript Task 1 completes and not before. Another note for futurereference: The precedence constraint shown here is the default OnSuccess type. This means thepreceding task Script Task 1 must complete andSucceed before the precedence will evaluate.

    The semantics here are important. The precedence constraint originatesat Script Task 1 andterminatesat Script Task 2. The originating task connects to the precedence constraintstartpointwhich has the half-bubble where it connects to the originating task. The endpointhas an arrow where it connects to the terminating task.

  • 8/13/2019 Level 6 Stairway to Integration Services

    12/12

    Precedence constraints evaluate. They test one or more conditions and, if the conditions aretrue, the constraint allows the terminating task to start execution.

    Stop the debugger.

    Single-Threading"

    Single-threading is one way to describe doing one thing at a time. Using a precedence constraint isone way to accomplish single-threading in the SSIS Control Flow. Using a precedence constraint isthe only way to accomplish deterministic workflow in the Control Flow.

    Is non-deterministic workflow possible in the SSIS Control Flow? Yep. To demonstrate, right-click theprecedence constraint connecting Script Task 1 to Script Task 2, and then click Delete. Click thewhitespace in the Control Flow and press the F4 key to display Package properties. TheMaxConcurrentExecutables property controls how many executables can execute concurrently in theSSIS Control Flow. By default, MaxConcurrentExecutables is set to -1. The default number ofconcurrent executables SSIS will allow is equal to the number of detected CPU cores plus two. Forexample, if you have a four-core server SSIS will permit six concurrent executables.

    Set the MaxConcurrentExecutables property to 1 as shown in Figure 22:

    Figure 22

    Re-execute the package in the SSIS debugger. Now, only one Script Task executes at a time, followedby the other. The order is non-deterministic and therefore unpredictable.

    Stop the debugger, reset the MaxConcurrentExecutables property to -1, and save your SSIS package.

    Conclusion

    In this article, we added a new SSIS package to our solution, built our first Script Tasks, andconnected them via an OnSuccess precedence constraint. We examined reading SSIS Variablevalues from inside an SSIS Script Task, and the MaxConcurrentExecutables property. In the nextStairway Step, we will look at more features of SSIS Precedence Constraints.

    Resources:My_First_SSIS_Project_After_Step_6.zip

    This article is part of the Stairway to Integration ServicesStairway