SSIS 2012 pass variable from child to parent package

徘徊边缘 提交于 2019-12-06 10:31:10

问题


I need to pull the value of a variable in a child package to the parent package. I can not get this to work.

I can set variables from parent to child using package configurations, but I cant find a way to get child value into parent. I tried using same process I used to set value from parent in child but it does not work.

The posted possible solution from another topic did not solve the problem it just stated it may not be possible. The post was from 2013 and a lot of things change, I wanted to see if this is possible now (without saving a value to an external table or anything like that).


回答1:


This child package is being used in a lot of spots, many of which would not have the parent variable I am trying to set (it would not exist in the parent variable). So the standard script in the post above would not work. I was hoping for a simple return variable value.

Using the above post as a starting point I updated the C# code to check to see if the variable I am trying to set in the parent package exists first (because it would not always be there), then if so set it.

Below is the code I came up with

        // 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();

        }



回答2:


You can put the variable in the parent package, and let the child package modify it.

Another sure-fire way is to populate a table in the child package and read the table in the parent package.



来源:https://stackoverflow.com/questions/39209753/ssis-2012-pass-variable-from-child-to-parent-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!