Chapter 6. DTS Workflows

DTS Workflows

Precedence constraints and steps are used to define the workflow in the DTS packages. Microsoft SQL Server provides three precedence constraints for use in designing the package workflow. Table 6.1 gives the types of constraints and the colors used to designate them in the DTS Designer window.

Table 6.1. DTS Precedence Constraints

Precedence Constraint

Arrow Color

On Completion

Blue

On Success

Green

On Failure

Red

How to Use DTS Workflows

To use the workflows within a DTS package, you should select two tasks in the DTS Designer and then select the precedence constraint you need from the Workflow menu or from the shortcut menu by right-clicking one of the tasks.

About the Order of the Tasks

When you create workflows, the order in which you select the tasks is important. First, you need to make sure that you select the source task and then the destination task to get the precedence restraint in the correct direction. You also must be aware that the order in which you set the tasks can affect the actual order in which the steps are executed.

After selecting the tasks, you will be able to select one of the three precedence-constraint options: On Completion, On Success, and On Failure (see Figure 6.1).

The DTS Designer window, showing workflows in the Workflow drop-down menu.

Figure 6.1. The DTS Designer window, showing workflows in the Workflow drop-down menu.

When you select a particular constraint, DTS inserts a line that has a color specific to the type of precedence, with an arrow signifying the direction of the workflow (refer to Table 6.1 earlier in this section).

Precedence Properties

If you right-click the workflow arrow, you can change the precedence properties of the workflow (see Figure 6.2).

The Precedence tab of the Workflow Properties dialog box.

Figure 6.2. The Precedence tab of the Workflow Properties dialog box.

Workflow precedence constraints have very few properties. You can change the Source Step, Precedence, and Destination Step settings. The source and destination steps are applied to the DTS tasks in the package. You can change the order of the workflow by switching the source and destination steps.

The other property that you can change is the precedence. You can choose among the three precedence constraints described in the following sections.

On Completion

You use this precedence constraint if you want to progress from step to step unconditionally. When you use this constraint, whether the source step succeeds or fails, the next step will execute when the source step completes.

On Success

You use this precedence constraint to execute the destination step only if the source step returns success. If the source step fails, all steps after the success constraint will not be executed. The On Success constraint allows you to establish steps that are required to be executed in a specific order.

Unexecuted Steps

In the progress dialog box, any steps that are not executed are represented by a black cross in a circle. Also note that these steps will not be recorded in the package log. (See Chapter 7, “Package,” for more information about the package log.)

On Failure

You use this precedence constraint to execute the destination step only if the source step fails. A good example of this use is when a step fails to send an email to the appropriate person. If a step fails, the email task would be executed.

Workflow Properties

You can access workflow properties by right-clicking a task and choosing Workflow Properties from the shortcut menu. This option is available for any task. The Precedence-tab properties were discussed earlier in this chapter. The precedence restraints that are displayed in the dialog box, however, are the ones that precede the selected step. You access the workflow properties in the Options tab (see Figure 6.3).

The Options tab of the Workflow Properties dialog box.

Figure 6.3. The Options tab of the Workflow Properties dialog box.

Transaction Properties

Some options for using transactions in your package can be defined in the steps. First, you can have a step join a DTS transaction if a transaction is available.

Enabling Transactions

Transactions must be enabled on the package for the workflow to join the transaction. Chapter 7 has more detailed information on enabling and using transactions in DTS.

Second, you can have the workflow cause all the transactions to commit on success or to roll back on failure by selecting the appropriate check boxes. These options affect the current package transaction and will commit or roll back all the pending steps to this point. If neither of these options is selected, the transaction will continue until the commit or rollback is called. For more information on transactions in a DTS package, see Chapter 7.

Transaction Commit Order

On commit, all steps will be executed in the order in which they were executed in the package.

Execution Properties

Several properties affect the execution of steps. Some of them affect the execution of the package as well, including how the step will be executed (threading instructions). Other properties are helpful in debugging a package. The following sections describe these properties.

Execute on Main Thread

You should use this option if you are using a data provider or executing objects that are not free-threaded. Examples of providers that are not free-threaded are Microsoft’s OLE DB Provider for Jet and most file-based data stores (such as Microsoft Excel, dBase, and HTML).

Warning from SQL BOL

Microsoft’s SQL Server Books Online (BOL) warns that serious errors will occur if you try to use parallel execution with Microsoft’s OLE DB Provider for Jet and most file-based data stores without setting Execute on Main Thread to TRUE.

Close Connection on Completion

When a package is executed, the connection will remain open, or when accessing a file, the Win32 file system will retain the file lock until the package has completed its execution. You can use this option to close connections or release file locks when the step has completed instead of waiting for the package to finish. You might want to use this option if you transfer data to a file (destination connection) and need to email it after you complete the data dump. In that case, you would want to close the connection, freeing the file lock to send it in the email message.

Some data providers benefit from closed connections. Also, some data providers perform better with fewer connections. Much of the time, however, the performance of a package is affected adversely when you close and open connections. You should refer to the documentation on the data provider to establish the best circumstances for using this option.

Fail Package on Step Failure

You use this option if you want to stop the entire package explicitly if this specific step fails. This property was introduced in SQL Server 2000. With its addition, you can use On Failure to branch the package workflow. If you are running multiple steps in parallel, this option also could be useful for stopping the entire package.

DSO Rowset Provider

This option, if selected, will expose OLE DB rowset data to external data consumers, such as the Query Analyzer (see Figure 6.4). When you use this option, the transformed data is returned to the calling program, not to the destination. Then you can use this feature to debug the transform before sending the data to the actual destination.

The Query Analyzer, retrieving data from the Transform Data Task when the DSO Rowset Provider option is turned on.

Figure 6.4. The Query Analyzer, retrieving data from the Transform Data Task when the DSO Rowset Provider option is turned on.

This option also might be helpful if you have a transform that you need to use in an application many times. Each time the call is made, the transform allows the data-transformation capabilities of the Transform Data Task to be used in many other programs.

You can access this data in two ways: querying the data by using the Transact SQL statement OPENROWSET, and joining to the package data by setting up the package as a linked server.

When you use the DSO Rowset Provider, be aware of the following:

  • The package from which the data is being queried must be executed from the calling program. The package cannot execute and send the data to a requesting consumer.

  • You can access data only from a step that is associated with a Transform Data Task.

  • You must be able to bind to destination columns, which means the destination cannot be a text file.

Enable this property only if you plan to use it. After the property is enabled, the Transform Data Task for which it is set will not run when the package is executed normally. Only OPENROWSET can access the data pump task while the DSO Rowset Provider property is enabled.

Querying Package Data

You can access data from Transform Data Tasks in a package in two ways, as described in the following sections.

Using OPENROWSET to Query Package Data

You can use the following syntax in Query Analyzer to query package data from a package stored in a file:

SELECT * FROM OPENROWSET('DTSPackageDSO','/F<pathname>','<query>') 

This syntax works with a package saved to SQL Server:

SELECT * FROM OPENROWSET('DTSPackageDSO', '/Usa /P /S /N<package name>','Select * ') 

The parameters used with OPENROWSET are as follows:

  • Provider name—. DTSPackageDSO. DTSPackageDSO is the DTS OLE DB Provider, and it is always used here.

  • Provider string—. /F<pathname> for files and /U<username> /P<password> /S<servername> /N<package name [/V<version>] for SQL Server packages. If you leave the server name blank, the local server is used. If you do not include a version, the last saved version will be used. The switches used here are the ones used with the dtsrun.exe utility. (See Table 7.4 in Chapter 7 for a complete list of parameters.)

  • Query—. Select *. This parameter is used to pass through the rowset data.

Using Distributed Queries to Query Package Data

First, you need to set up the package as a linked server. as follows:

sp_addlinkedserver '<servername>','<packagename>','DTSPackageDSO','<provider string>) 

The parameters are as follows:

  • Server name—. The name you choose for the linked server.

  • Package name—. In this context, anything you want to use, including a null string.

  • Provider name—. DTSPackageDSO. DTSPackageDSO is the DTS OLE DB Provider, and it is always used here.

After you set up the linked server, you can use it in a distributed query like the following:

SELECT i.ItemID, i.ItemName, w.Location 
FROM Inventory AS i, dtsLinkedServerName…WarehousePackageStep as w 
WHERE i.ItemID = w.ItemID 

For more information, search for OPENROWSET in SQL Server BOL and MSDN Online.

Disable This Step

You use this option to disable this workflow step when the package is executed. This property is extremely helpful when you are trying to debug a package.

Task Priority

You can set the thread priority in Windows for this step. The default is normal while low and high priorities are available.

ActiveX Script

You can use ActiveX scripting in the workflow to exercise greater control over the execution of the steps in a package. Although ActiveX scripting in the workflow allows you to stop, restart, and even loop package execution, its primary goals are initialization and preventing a step from executing. Several constants are available to help you program the execution or disabling of steps (see Table 6.2).

Table 6.2. ActiveX Script Constants for DTS Workflows [1]

Constant

Value

Description

DTS STEP SCRIPT RESULT CONSTANT

NA

These constants are the possible results that can be returned to the step via the ActiveX script. The DTS Step Script Result constants should be used only in DTS workflow scripts, not in scripts associated with an ActiveXScriptTask object or DataPumpTransformScript or DTSTransformScriptProperties2 transformations.

DTSStepScriptResult_ DontExecuteTask

1

When used in scripting, this constant will not run the task associated with this step. In the status window for the package, the task will be shown as Not Run. Please note that any step not run will not be logged in the package log, because DTS does not log steps that are not executed.

DTSStepScriptResult_ExecuteTask

0

This constant causes the step to execute immediately.

DTSStepScriptResult_RetryLater

2

This constant tries to execute the step later.

DTS STEP EXECUTION STATUS CONSTANTS

NA

You can set or return the status of the task associated with the step here.

DTSStepExecStat_Completed

4

The step has been executed and is marked as complete.

DTSStepExecStat_Inactive

3

The step is inactive and will not be run.

DTSStepExecStat_InProgress

2

The step is in the process of being executed. This constant can be useful with steps that take awhile to complete.

DTSStepExecStat_Waiting

1

The step is waiting to execute. It will execute upon its turn in the workflow.

DTS STEP EXECUTION RESULT CONSTANTS

NA

These constants are the return values for completed steps.

DTSStepExecResult_Failure

1

The step failed.

DTSStepExecResult_Success

0

The step succeeded.

[1] Some of the information in this table is from Microsoft SQL Server 2000 Version 8.00 BOL. This information is available on the SQL Server installation CD.

See SQL Server BOL for more constants for workflow and precedence. These constants would be most useful if you are using Visual Basic to program the package. The constants included in Table 6.2 are the ones most likely to be used in workflow scripting.

The following sections describe the scripting options.

Setting up the Sample Package

This package is simple, because the goal is to illustrate how workflow scripting is set up and how it reacts in some situations. Although the package is simple, you should be able to apply the principles to any package in which you need to have added control of workflows.

Create a new package, and add three ActiveX script tasks. Name these tasks Step 1, Step 2, and Step 3 (see Figure 6.5).

The workflow scripting package.

Figure 6.5. The workflow scripting package.

Listings 6.1, 6.2, and 6.3 show the code for these tasks.

Example 6.1. Step 1 Code

Function Main() 
   msgbox "Step 1 Running" 
   DTSGlobalVariables("attempts").Value = 0 
   Main = DTSTaskExecResult_Success 
End Function 

Example 6.2. Step 2 Code

Function Main() 
   msgbox"Step 2 Running" 
   Main = DTSTaskExecResult_Success 
End Function 

Example 6.3. Step 3 Code

Function Main() 
   msgbox "Step 3 Running" 
   Main = DTSTaskExecResult_Success 
End Function 

Now that the package is set up, you can begin working with the ActiveX scripts in the steps’ workflow properties.

Stopping Execution

A natural use of workflow scripting is to verify whether a file exists before the Transform Data Task loads it. The VBScript in Listing 6.4 verifies that a file called Manufacturing.mdb exists in the C:DTS2000Data directory [2] before it lets the step to which it is tied execute, thus allowing the rest of the package to execute.

Example 6.4. VB Script in Workflow That Checks to See Whether a File Exists Before Letting the Task Execute

Function Main() 
      Dim fso 
      Set fso = CreateObject("Scripting.FileSystemObject") 
      If (fso.FileExists("C:DTS2000DataManufacturing.mdb")) Then 
            Main = DTSStepScriptResult_ExecuteTask 
      Else 
            Main = DTSStepScriptResult_DontExecuteTask 
      End If 
End Function 

This code uses the File System object to verify that the Access database exists where it is supposed to exist. If this database does exist, the step executes. Otherwise, the code tells the step not to execute, and all other steps are not executed. If you have other independent workflows running, they will continue to run as expected.

This method would allow you to transform from multiple data sources without causing the package to fail if one is incorrect. To test this method, add the code in the following section to the workflow properties ActiveX script for Step 1.

Restarting Execution

Sometimes during a package execution, you need to pause until a certain condition becomes true before you can continue. The script in Listing 6.5 polls for a file, pauses five seconds, and tries again. After five attempts to find the file, the script fails. To demonstrate that fact, the file in this example does not exist.

To try this example, remove the script from the workflow properties ActiveX script in Step 1, and add the script in Listing 6.5 to the workflow properties ActiveX script in Step 2.

Example 6.5. VBScript in Workflow to Restart a Package After a Specified Time Delay and Several Retries

Function Main() 
Function Main() 
      Dim oPackage 
      Dim CurrentTime 
      Dim NextTime 
      Dim fso 

      Set fso = CreateObject("Scripting.FileSystemObject") 

      DTSGlobalVariables("attempts").Value = DTSGlobalVariables 
("attempts").Value + 1 
      CurrentTime = Now() 
      NextTime = dateadd("s", 5, Now()) 

      Set oPackage = DTSGlobalVariables.Parent 
      msgbox "Initial Value #" & DTSGlobalVariables("attempts").value 

      'Test to see if the file exists 
If (fso.FileExists("C:MyFile")) Then 
            'If it does, execute the package 
            msgbox "Execute Package" 
            Main = DTSStepScriptResult_ExecuteTask 
      Else 
            'If it doesn't, retry in increments of 5 seconds 5 times 
            If DTSGlobalVariables("attempts").Value < 6 Then 
                  do until (CurrentTime > NextTime) 
                        CurrentTime = Now() 
                  loop 
                  msgbox "Retry #" && DTSGlobalVariables("attempts").value 
                  Main = DTSStepScriptResult_RetryLater 
            Else 
                  'Stop execution once retries = 5 
                  msgbox "Execution Stopped at 5 retries" 
                  DTSGlobalVariables("attempts").Value = 0 
                  Main = DTSStepScriptResult_DontExecuteTask 
            End If 
      End If 
End Function 

As you read through the code, you will see that it uses two global variables. If the variables don’t exist, DTS will create them when they are first used.

Looping Execution

This section illustrates how to create a loop condition in the workflow. By adding the script in Listing 6.6 to the workflow properties ActiveX script for Step 3, you will see how a loop can include more steps than the ones directly related to the calling step. (Be sure to deselect the workflow properties ActiveX script option in Step 2 before running this example.) Similar to the restart script, the loop script uses a global variable to hold an integer for counting. If the counter has not reached 5, Step 3 is not executed, and Step 1 is set to waiting, which causes Steps 1 and 2 to be executed again. (Step 2 is executed because it comes between Step 1 and Step 3 in the workflow.) The workflow will continue to loop until the counter reaches 5.

Example 6.6. VBScript in Workflow That Loops a Package Five Times

Function Main() 
   Dim oPkg 
   DTSGlobalVariables("counter").Value = DTSGlobalVariables("counter").Value + 1 
   If DTSGlobalVariables("counter").Value < 5 Then 
     Msgbox "Counter" & DTSGlobalVariables("counter").Value 
     Set oPkg = DTSGlobalVariables.Parent 
     'Set step 1 status to waiting. 
    oPkg.Steps("DTSStep_DTSActiveScriptTask_1").ExecutionStatus = 
       DTSStepExecStat_Waiting 
      'Do not execute task 3, step 1 will restart. 
      Main = DTSStepScriptResult_DontExecuteTask 
    Else 
      'Execute task 3 
      Main = DTSStepScriptResult_ExecuteTask 
    End If 
End Function 

For another example of looping, see Chapter 15, “Managing Distributed Databases with DTS and Message Queues.”

Looping in the Workflow

Although many people use the workflow for looping, as illustrated in this section, you need to be careful when implementing it. The implementation of looping in the workflow ActiveX script can have side effects. If possible, you should use an ActiveX Script Task or Dynamic Properties Task to implement looping in a DTS package. See Chapter 12, “Building a Data Warehouse with DTS and Analysis Services,” for an example of both tasks.

Workflow Scripting Tips

The following are a couple of tips to keep in mind when you are working with workflows:

  • Workflow properties are available for any step in a package.

  • Although the Data Transformation Task is represented by a line and arrow, it is not considered to be a workflow by itself; it is a step in the workflow. You still can assign it precedence, however, when you use a single connection for the destination in one step and the source in the next step. This procedure changes the color of the line used to represent the Data Transformation Task accordingly.

By using workflow scripting, you can exercise greater control of the execution of steps in a package, which allows you to validate and even prepare data for use in the tasks being executed by the steps.

Summary

This chapter explained the components of the DTS workflow, along with steps and precedence constraints. You saw how the precedence constraints could be used to maintain data integrity, validate data, and notify users if necessary. You also saw how the workflow can be used to set up and participate in transactions. In addition, you saw how to use ActiveX scripts to control the workflows you are using, including stopping, restarting, and looping through workflows. When you understand how the workflow and its components function, you will have greater control of the execution of your package.



[2] All data files and scripts used in this book are available to be downloaded from http://www.magenic.com/publications or http://www.newriders.com. The self-extracting ZIP file will create a directory structure starting with DTS2000. You can extract the files to any drive or directory, but all file references in this book will be based on C:DTS2000.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.21.205