SSIS 2012 将变量从子包传递到父包
SSIS 2012 pass variable from child to parent package
我需要将子包中的变量值拉取到父包中。我无法让它工作。
我可以使用包配置设置从父项到子项的变量,但我找不到将子项值传递给父项的方法。我尝试使用与从父项到子项设置值相同的过程,但它不起作用。
来自另一个主题的 posted 可能的解决方案并没有解决它刚才所说的可能不可能的问题。 post 是从 2013 年开始的,很多事情都发生了变化,我想看看现在是否可行(无需将值保存到外部 table 或类似的东西)。
可以把变量放在父包里,让子包去修改。
另一种万无一失的方法是在子包中填充一个table并在父包中读取table。
这个子包被用在很多地方,其中很多地方没有我试图设置的父变量(它不会存在于父变量中)。所以上面 post 中的标准脚本将不起作用。我希望有一个简单的 return 变量值。
使用上面的 post 作为起点,我更新了 C# 代码以检查我试图在父包中设置的变量是否首先存在(因为它不会总是存在),如果是这样设置它。
下面是我想出的代码
// have to do this FIRST so you can access variable without passing it into the script task from SSIS tool box
// Populate collection of variables.
//This will include parent package variables.
Variables vars = null;
Dts.VariableDispenser.GetVariables(ref vars);
// checks if this variable exists, and if so then will set it to the value of the child variable
if (Dts.VariableDispenser.Contains("ParentVar") == true)
{
//MessageBox.Show("ParentVariableExists");
// Lock the to and from variables.
// parent variable
Dts.VariableDispenser.LockForWrite("User::ParentVar");
// child variable
Dts.VariableDispenser.LockForRead("User::ChildVar");
// Apparently need to call GetVariables again after locking them.
// Not sure why - perhaps to get a clean post-lock set of values.
Dts.VariableDispenser.GetVariables(ref vars);
// parentvar = childvar
vars["User::ParentVar"].Value = vars["User::ChildVar"].Value;
vars.Unlock();
}
我需要将子包中的变量值拉取到父包中。我无法让它工作。
我可以使用包配置设置从父项到子项的变量,但我找不到将子项值传递给父项的方法。我尝试使用与从父项到子项设置值相同的过程,但它不起作用。
来自另一个主题的 posted 可能的解决方案并没有解决它刚才所说的可能不可能的问题。 post 是从 2013 年开始的,很多事情都发生了变化,我想看看现在是否可行(无需将值保存到外部 table 或类似的东西)。
可以把变量放在父包里,让子包去修改。
另一种万无一失的方法是在子包中填充一个table并在父包中读取table。
这个子包被用在很多地方,其中很多地方没有我试图设置的父变量(它不会存在于父变量中)。所以上面 post 中的标准脚本将不起作用。我希望有一个简单的 return 变量值。
使用上面的 post 作为起点,我更新了 C# 代码以检查我试图在父包中设置的变量是否首先存在(因为它不会总是存在),如果是这样设置它。
下面是我想出的代码
// have to do this FIRST so you can access variable without passing it into the script task from SSIS tool box // Populate collection of variables. //This will include parent package variables. Variables vars = null; Dts.VariableDispenser.GetVariables(ref vars);
// checks if this variable exists, and if so then will set it to the value of the child variable
if (Dts.VariableDispenser.Contains("ParentVar") == true)
{
//MessageBox.Show("ParentVariableExists");
// Lock the to and from variables.
// parent variable
Dts.VariableDispenser.LockForWrite("User::ParentVar");
// child variable
Dts.VariableDispenser.LockForRead("User::ChildVar");
// Apparently need to call GetVariables again after locking them.
// Not sure why - perhaps to get a clean post-lock set of values.
Dts.VariableDispenser.GetVariables(ref vars);
// parentvar = childvar
vars["User::ParentVar"].Value = vars["User::ChildVar"].Value;
vars.Unlock();
}