When you execute a Data Transformation Services (DTS) package, you can save data or a value from a DTS step to pass on to subsequent steps. For example, you can use the saved data to change the way a subsequent step executes or to dynamically modify a SELECT statement.
When you use global variables with DTS tasks, you can:
Alternatively, the global variable can be used in a WHERE clause to determine the specific records to be loaded (for example, only loading up records that were modified on that month-end date).
You can then use the global variable as an in-memory lookup table. With the Execute SQL task, issue a SELECT statement against a state table and store the results in a global variable. Then, with the Transform Data task, for each source row, call a "StateLookup" function that iterates through the global variable recordset and matches the state code in the source field with the state code in the table. When there is a match, store the full name of the state in the destination column. For more information, see Lookup Queries.
You can create global variables in DTS and assign them values in the following ways:
Scope refers to the lifetime of the variable reference in memory. The scope depends on where the variables are declared or initialized. Whether a global variable is still accessible after a package has executed depends on how the global variable was created.
A global variable created during design time retains the value it had when the package finished execution, if the package is saved. For example, suppose you create the global variable, "city," and set it to the value of "Boston." During package execution, an ActiveX script changes the value of "Boston" to "Philadelphia." The next time you execute the package, the global variable will contain "Philadelphia," not "Boston." This is useful if you want to query the value of a package global variable after execution.
However, global variables created dynamically in an ActiveX script have two scopes. If they are created above the Function Main(), they are available to all functions in the script. This is the equivalent to module-level scope. If they are declared within a function, they are available only inside that function. This is equivalent to procedure-level scope.
The following examples show you how to create, set, retrieve, and use the values of global variables in a DTS package by using an ActiveX script.
If a global variable does not exist when the package is run, you can create one dynamically by using an ActiveX script. To create a new global variable called "city" and assign it a value of "Boston", use the following Microsoft Visual Basic® Scripting Edition (VBScript) code:
DTSGlobalVariables("city").value = "Boston"
You can dynamically create a COM object and store it in a global variable from within an ActiveX script. In the following example, VBScript code is used to create an ADO connection, which can be used by scripts in the package to execute SQL commands and examine ADO recordsets. In this example, a year-to-date sales table containing a Totals field in the pubs database is created:
Function Main()
dim conn
set DTSGlobalVariables("MyConn").value = CreateObject("ADODB.Connection")
set conn = DTSGlobalVariables("MyConn").value
conn.provider="sqloledb"
conn.open "(local)", "sa", ""
conn.DefaultDatabase = "pubs"
conn.execute("Create Table YTDSales (Totals int)")
Main = DTSTaskExecResult_Success
End Function
The following code example shows you how to create the year-to-date sales table using Microsoft JScript®:
function Main()
{
DTSGlobalVariables("MyConn").value = CreateObject("ADODB.Connection");
conn = DTSGlobalVariables("MyConn").value;
conn.open = ("provider = sqloledb; data source = (local);user id = sa");
conn.DefaultDatabase = "pubs";
conn.execute("Create Table YTDSales (Totals int)");
return(DTSTaskExecResult_Success)
}
The following ActiveX script code, written in VBScript, sets the value of a global variable named count to 200:
DTSGlobalVariables("count").value = 200
The following ActiveX script code, written in VBScript, gets the value of a global variable named count and saves the value in a variable named globalCount:
globalCount = DTSGlobalVariables("count").value
The following code concatenates the value of a column containing a file name with a global variable containing the Julian date, and stores the new results in the filename column in a destination table:
DTSDestination("FileName") = DTSSource("FileName") & DTSGlobalVariable("julianDate").value