Reputation: 2867
I have a script component in SSIS on SQLServer 2008 R2, that needs to be able to write to a Read-Write variable to produce a file name for a flat file export. I created a package level variable to hold the file name, and set the flat file connection to use an expression containing the variable..
I have a script component that, among other things, builds the file name dynamically in the post execute method. I've set the variable in the ReadWriteVariables setting of the script component.
The package will immediately fail if I don't have a default value in the variable, because the flat file connection manager tries to evaluate the expression to set up the destination file. So, I just put in a placeholder file name.
The problem is that now it always uses the placeholder filename instead of the one that the script specifies. What's the best way to make sure that I can write to those variables? I tried Variables.VariableName = "value", I've also tried using VariableDispenser and this.ReadWriteVariables["VariableName"].value, and none of them are persisting the value I set in the script.
Upvotes: 1
Views: 5331
Reputation:
Here is one way you can assign value to a package variable from within Script Component
available inside Data Flow Task
.
I try to lock the variables inside the Script Task
or Script Component
instead of specifying them on Properties dialog. I feel this is easier to maintain.
In the following example, I have a package variable named FileName and the variable is being assigned with the value C:\Temp\PathChanged
inside the Script Component.
I believe that Script Component may not be the right place to manipulate the package variable value such as file name but again that depends on what you are trying to do.
Hope that helps.
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForWrite("User::FileName");
this.VariableDispenser.GetVariables(out varCollection);
varCollection["User::FileName"].Value = @"C:\Temp\PathChanged";
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
}
}
Upvotes: 1