Tuesday, March 15, 2011

Skipping Task when Empty File

At times just checking for a file existence may not enough, if the file exists but is empty the load task may need to be skipped. I have compiled the example below to show one method of skipping the DTS task on file empty. The example utilizes the Workflow Properties page of each DTS Task. Although most corporations are moving away from DTS in favor of new ETL work being done in SSIS, SQL Server Integration Services, there are still plenty of opportunites to improve DTS packages that are still running. This example only works if the load task is the only task in the DTS package. I’ll be posting an update later with an example of how to move to the next task without executing the current task.

The workflow properties tab is available on the following components in the DTS Task pane. Bulk Insert Task, ActiveX Script Task, Execute SQL Task and Data Driven Qeury Task. The option is located by right-clicking the component, selecting Workflow and then Properties. Once on the Workflow Property tab select Options, check ‘Use Active X Script’ and then click properties. The mentioned sequence will open an Active X Script task window. This script is executed prior to the main body of the task.

The code below opens a file that is defined in the global variable, CheckFIle. This is the full file path including name to be checked. If the file is empty or has a blank first line the task is skipped. The result in a job is that the step reports success.

'**********************************************************************
' Skip task if empty file or first line is blank, uses global variable CheckFile
'************************************************************************
Function Main()
   SET fso = CreateObject("Scripting.FileSystemObject")
   DataSetNameTxt = DTSGlobalVariables("CheckFile").value
   Set TextStream = fso.OpenTextFile(DataSetNameTxt,1)
   If TextStream.AtEndOFStream or TextStream.AtEndOfLine then
      Main=DTSStepScriptResult_DontExecuteTask
   else
      Main=DTSStepScriptResult_ExecuteTask
  end if
  TextStream.Close
  Set TextStream = nothing
End Function

By returning the value of either DontExecuteTask, skip the main task, or ExecuteTask proceed normally, execution the main body of the task is controlled.

Below are the screenshots to show the path to Workflow Properties Task input window:


No comments:

Post a Comment