SSIS Script Task Assign Value to Variable
SSIS SCRIPT TASK
SCRIPT TASK (.NET)
The Script Task enables you to access the Microsoft Visual Studio Tools for Applications (VSTA) environment to write and execute scripts using the VB and C# languages. Using this task, you can create additional logic that the canned SSIS tasks can’t accomplish.
Scripting now is almost a misnomer because the latest SSIS edition solidifies the connection to the full .NET 4.0 libraries for both VB and C#. The latest addition to SSIS of the VSTA environment and the Script Task, in general, also offer these extra functional advantages:
- A coding environment with the advantage of IntelliSense
- An integrated Visual Studio design environment within SSIS
- An easy-to-use methodology for passing parameters into the script
- The capability to add breakpoints to your code for testing and debugging purposes
- The automatic compiling of your script into binary format for increased speed
The Script Task is configured through the Script tab in the Script Task Editor (shown in below diagram). The ScriptLanguage property is where you select the .NET language you want to use in the task. Notice that the default language is set to C#, so if you are coding in VB.NET, don’t whiz through these settings when setting up your Script Tasks.
The EntryPoint property enables you to provide an alternative function to call initially when the ScriptMain class is instantiated. Typically, you’ll leave this set to the default Main() function. The ReadOnlyVariables and ReadWriteVariables properties enable you to pass SSIS variables into the script as a listing of variable names separated by commas. While this is not the only way to pass variables into a script, it is the easiest. Having these variables provides a significant advantage when coding. You only need to refer to them by ordinal position or by name in the Variable collection to be able to access their values without worrying about locking, unlocking, or blocking variables during read and write actions. Just make sure that any variables you wish to write back to in the script code are listed in the ReadWriteVariables property; otherwise, you’ll get an error in the script. There are alternative methods for altering variables that aren’t provided in these collections during setup; these are demonstrated in Chapter 9, which covers scripting.
When you click the actionable Edit Script button, the Visual Studio Tools for Applications environment opens to allow coding directly in the class ScriptMain. In this IDE, you have access to all the advanced debugging tactics, breakpoints, and IntelliSense found in the Visual Studio environment. If you create a package with a variable named myValue containing the string “Hello World”, and set up the Script Task as shown in below diagram, the following example shows you how to write code that uses the passed-in myValue variable:
public void Main()
ystem.Windows.Forms.MessageBox.Show(“MyValue=” + Dts.Variables
ts.TaskResult = (int)ScriptResults.Success;
Public Sub Main()
If Dts.Variables.Contains(“User::MyValue”) = True Then
System.Windows.Forms.MessageBox.Show(“myValue=” & Dts.Variables
Dts.TaskResult = ScriptResults.Success
First, the script (above diagram) checks for the existence of the variable, and then pops up a message box with the famous message “Hello World.”
This is just a very simple example of the Script Task in action. We’ve created an entire chapter to dive into the details and provide specific use cases for both the Script Task and the Data Flow version called the Script Component, so see Chapter 9 for more information.