Each container has its own Variables collection. When a new variable is created, it is within the scope of its parent container. Because the package container is at the top of the container hierarchy, variables with package scope function like global variables, and are visible to all containers within the package. The collection of variables for the container can also be accessed by the children of the container through the Variables collection, by using either the variable name or the variable's index in the collection.
Because the visibility of a variable is scoped from the top down, variables declared at the package level are visible to all the containers in the package. Therefore, the Variables collection on a container includes all the variables that belong to its parent in addition to its own variables
Conversely, the variables that are contained in a task are limited in scope and visibility, and are only visible to the task.
If a package runs other packages, the variables defined in the scope of the calling package are available to the called package. The only exception occurs when a same-named variable exists in the called package. When this collision occurs, the variable value in the called package overrides the value from the calling package. Variables defined in the scope of the called package are never available back to the calling package.
The following code example programmatically creates a variable, myCustomVar, at the package scope, and then iterates through all the variables visible to the package, printing their name, data type, and value.
using System;
using Microsoft.SqlServer.Dts.Runtime;
namespace Microsoft.SqlServer.Dts.Samples
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
// Load a sample package that contains a variable that sets the file name.
Package pkg = app.LoadPackage(
@"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" +
@"\Package Samples\CaptureDataLineage Sample\CaptureDataLineage\CaptureDataLineage.dtsx",
null);
Variables pkgVars = pkg.Variables;
Variable myVar = pkg.Variables.Add("myCustomVar", false, "User", "3");
foreach (Variable pkgVar in pkgVars)
{
Console.WriteLine("Variable: {0}, {1}, {2}", pkgVar.Name,
pkgVar.DataType, pkgVar.Value.ToString());
}
Console.Read();
}
}
}
Imports Microsoft.SqlServer.Dts.Runtime
Module Module1
Sub Main()
Dim app As Application = New Application()
' Load a sample package that contains a variable that sets the file name.
Dim pkg As Package = app.LoadPackage( _
"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" & _
"\Package Samples\CaptureDataLineage Sample\CaptureDataLineage\CaptureDataLineage.dtsx", _
Nothing)
Dim pkgVars As Variables = pkg.Variables
Dim myVar As Variable = pkg.Variables.Add("myCustomVar", False, "User", "3")
Dim pkgVar As Variable
For Each pkgVar In pkgVars
Console.WriteLine("Variable: {0}, {1}, {2}", pkgVar.Name, _
pkgVar.DataType, pkgVar.Value.ToString())
Next
Console.Read()
End Sub
End Module
Sample Output:
Variable: CancelEvent, Int32, 0
Variable: CreationDate, DateTime, 4/18/2003 11:57:00 AM
Variable: CreatorComputerName, String,
Variable: CreatorName, String,
Variable: ExecutionInstanceGUID, String, {237AB5A4-7E59-4FC9-8D61-E8F20363DF25}
Variable: FileName, String, Junk
Variable: InteractiveMode, Boolean, False
Variable: LocaleID, Int32, 1033
Variable: MachineName, String, MYCOMPUTERNAME
Variable: myCustomVar, String, 3
Variable: OfflineMode, Boolean, False
Variable: PackageID, String, {F0D2E396-A6A5-42AE-9467-04CE946A810C}
Variable: PackageName, String, DTSPackage1
Variable: StartTime, DateTime, 1/28/2005 7:55:39 AM
Variable: UserName, String, <domain>\<userid>
Variable: VersionBuild, Int32, 198
Variable: VersionComments, String,
Variable: VersionGUID, String, {90E105B4-B4AF-4263-9CBD-C2050C2D6148}
Variable: VersionMajor, Int32, 1
Variable: VersionMinor, Int32, 0
Notice that all the variables scoped in the System namespace are available to the package. For more information, see System Variables.