SQL Server 2000 DTS Part 9 - DTS Designer Tasks - Page 2February 10, 2004 As you can see within the displayed window, the multiphase data pump is divided into the following phases and sub-phases:
Division of processing into phases provides several benefits. It offers more control over data manipulation as well as error handling and recovery. Transformation can be restarted from the point of failure, without reloading already processed data. Let's take a look at a sample ActiveX Script demonstrating the multi-phase capabilities of the Transform Data Task. For the sake of example, let's assume we will use an existing transform, producing table consisting of the content of the Shippers table with the total number of orders for each ShipperID, using the batch size of two (which should produce two batches). We will record the processing information in a text file (which name is stored in a global variable "LogFile") including such values as "TotalBatches", "TotalRows", and "FailedRows". Prior to any customization, auto generated code of our ActiveX Script transformation has the following format (Function Main is executed during the Row Transform phase):
Function PreSourceMain()
PreSourceMain = DTSTransformstat_OK
End Function
Function Main()
DTSDestination("Orders") = DTSSource("Orders")
DTSDestination("Phone") = DTSSource("Phone")
DTSDestination("CompanyName") = DTSSource("CompanyName")
DTSDestination("ShipperID") = DTSSource("ShipperID")
Main = DTSTransformStat_OK
End Function
Function TransFailureMain()
TransFailureMain = DTSTransformstat_OK
End Function
Function InsertSuccessMain()
InsertSuccessMain = DTSTransformstat_OK
End Function
Function InsertFailureMain()
InsertFailureMain = DTSTransformstat_OK
End Function
Function BatchCompleteMain()
BatchCompleteMain = DTSTransformstat_OK
End Function
Function PumpCompleteMain()
PumpCompleteMain = DTSTransformstat_OK
End Function
Function PostSourceMain()
PostSourceMain = DTSTransformstat_OK
End Function
Start by creating the "LogFile" global variable from the DTS Package Properties dialog box (you can find more information on the subject in one of our earlier articles) of type string and set its value appropriately (e.g. "d:\logs\MPDPump.log"). Then modify the ActiveX Script so it matches the following code:
' Copy each source column to the destination column
Function PreSourceMain()
Const Overwrite = TRUE
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.CreateTextFile(DTSGlobalVariables("LogFile").Value, Overwrite)
DTSGlobalVariables("TotalBatches").Value = 0
DTSGlobalVariables("TotalRows").Value = 0
oLogFile.Close
Set oFSO = Nothing
PreSourceMain = DTSTransformstat_OK
End Function
' Copy each source column to the destination column
Function Main()
DTSDestination("Orders") = DTSSource("Orders")
DTSDestination("Phone") = DTSSource("Phone")
DTSDestination("CompanyName") = DTSSource("CompanyName")
DTSDestination("ShipperID") = DTSSource("ShipperID")
DTSGlobalVariables("TotalRows").Value = _
DTSGlobalVariables("TotalRows").Value + 1
MsgBox "Row #: " & DTSGlobalVariables("TotalRows").Value
Main = DTSTransformStat_OK
End Function
Function TransFailureMain()
TransFailureMain = DTSTransformstat_OK
End Function
Function InsertSuccessMain()
InsertSuccessMain = DTSTransformstat_OK
End Function
Function InsertFailureMain()
InsertFailureMain = DTSTransformstat_OK
End Function
Function BatchCompleteMain()
DTSGlobalVariables("TotalBatches").Value = _
DTSGlobalVariables("TotalBatches").Value + 1
MsgBox "Batch #: " & DTSGlobalVariables("TotalBatches").Value
BatchCompleteMain = DTSTransformstat_OK
End Function
Function PumpCompleteMain()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile(DTSGlobalVariables("LogFile").Value, ForWriting)
oLogFile.WriteLine "Total Rows:" & vbTab & DTSGlobalVariables("TotalRows").Value
oLogFile.WriteLine "Total Batches:" & vbTab & DTSGlobalVariables("TotalBatches").Value
oLogFile.Close
Set oFSO = Nothing
PumpCompleteMain = DTSTransformstat_OK
End Function
Function PostSourceMain()
PostSourceMain = DTSTransformstat_OK
End Function
This trivial example, which displays rows and batch numbers as they are being processed, and writes the total number of batches and rows to a log file, should help you to create scripts that are more elaborate. Refer to the Phased Transformation Samples topic in the SQL Server 2000 Books Online for additional help on this subject (you will find there samples demonstrating TransformFailed and InsertFailed functions). In our next article, we will cover the remaining two DTS tasks. |