SSIS: Set parent package variable from child package

SSIS: Set parent package variable from child package

For this sample we communicate using bool value named ReturnValue
First create a variable in parent package: ReturnValue as Boolean
In child package, you do not need to set package configuration for the parent variable, nor should the ReturnValue variable be created.
Create script task in child package.
Leave the fields ReadOnlyVariables, ReadWriteVariables empty.

public void Main()
{
   
    Variables vars = null;
    Dts.VariableDispenser.GetVariables(ref vars);

    var dummy = false;
           
    if (Dts.VariableDispenser.Contains("User::ReturnValue"))
    {
        Dts.Events.FireInformation(0, "", "Setting (parent package variable) User::ReturnValue", null, 0, ref dummy);
        Dts.VariableDispenser.LockForWrite("User::ReturnValue");
        Dts.VariableDispenser.GetVariables(ref vars);

        vars["User::ReturnValue"].Value = true;

        vars.Unlock();
    }
    else
    {
        Dts.Events.FireInformation(0, "", "User::ReturnValue not found. Skipping (this is normal when child package isexecuted on its own)", null, 0, ref dummy);
    }
                        
    Dts.TaskResult = (int)ScriptResults.Success;
}

Comments

Popular posts from this blog

How to decrypt stored password from SSMS registered servers

Insert bulk statement does not support recompile (SQL 2017)