Welcome to blogs.conchango.com Sign in | Join | Help

Welcome to blogs.conchango.com

SSIS Junkie

SSIS: Passing variables between packages

A question was raised on a beta newsgroup earlier today concerning how to use variables across different packages when the Execute Package task is being used. There was some confusion regarding under what circumstances a package can read/write a variable in another package.

The hard and fast rule is this: A child package has access to variables in the parent package (i.e. the package that called it), but not the other way round. A parent package cannot see variables that are defined in a child package.

If you think about variable scope this makes sense. For example, a variable that is scoped to a Foreach Loop container cannot be seen by anything outside of that container. Its the same when the container is a package...remember that packages calling child packages is just an extension of the container hierarchy. A package is in itself a container and is not necassarily the root of a container hierarchy, it is merely a logical unit of functionality for manageability.

There are some caveats to this rule. Not all tasks in a child package have access to variables in a parent package. For example an Execute SQL task can't populate a variable in  parent package via the ResultSet tab. [I have opened a bug on this particular scenario and am hoping it will be fixed by RTM - though I doubt it]. The workaround is to populate a local variable and then use a script task to assign the value of that local variable to the variable in the parent package.

Parent package configurations can be used to produce an almost opposite behaviour. They are used to receive values from a parent package IF the package has been called by a parent package. I like to use the analogy that this is like passing ByValue whereas the scenario explained above is a bit like passing ByRef.

This is a short and sweet overview of how variables can be used between packages. If you have any questions, please feel free to post them as a comment.

-Jamie

 

 

Published 17 March 2005 21:17 by jamie.thomson

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

jason said:

Afternoon Jamie,
I have run into a situation where I have two packages. The parent sets a varDir=RSdirectory and executes a EPT (exe pack task). In my child package, I have a foreach container expr to set the directory to the parent varDir.

The problem here is that I cannot see my parent variable. Even when I try to code it manually, I get an error stating that it doesn't exist in scope.

However, according to your blog this is allowed, is there something I am missing here?
If its not too much trouble could you maybe post ashort example of how to do this?

Using June CTP SSIS...

Thanks in advance,
Jason W.
July 20, 2005 21:10
 

jamie.thomson said:

Hi Jason,
Yeah, this is the anomoly I aluded to above where the Execute SQL Task cannot see a variable in the parent package - the same is true of the Foreach container. I don't have a definitive list of all the tasks/containers that cannot see a variable in the parent package but I would say it is all of them - possibly all but the script task.

The workaround is to populate a local variable and use the script task to assign the value to a variable in the parent package.

You may, quite reasonably, ask why I didn't say all this in the original post. Well its because I'm not yet sure whether this is a bug or whether it is by design. I have raised the issue with the dev team and they say they will address this later (after RTM) and so far they have not been forthcoming as to whether this is a bug or not so don't assume that this behaviour will ever change.

In my opinion it IS a bug. If what they say about the container hierarchy is true (that a child package is just a logical extension of the parent package's container hierarchy) then there is no reason that it shouldn't be allowed. The counter argument is that at design time the child package is just a stand-alone package and doesn't know that at runtime that it is going to be called from elsewhere therefore it cannot validate that the variable exists.

Hope that makes sense.

-Jamie
July 20, 2005 21:38
 

jason said:

Thanks Jamie for your quick response.

I think I must be crazy but I am still getting errors when assigning the var in the script task. Woul dit be possible to show me a quick example of how to do this?

Thanks for humoring me:)
Jason
July 20, 2005 22:30
 

jamie.thomson said:

Jason,
Not a problem. Drop me an email via the Contact link over to the left there and I'll send you something over. I'd post it up here but we're having issues with our blogging engine at the moment which means I can't upload files to my blog (which is why there hasn't been much content recently).

-Jamie
July 20, 2005 22:35
 

jason said:

Hello again Jamie,
I just wanted to drop you a line and let you know I found another way to pass variables without setting in script objects, it has been staring me in the face this whole time. go to package configuration organizer -> add new package config, set the config type: "parent package variable" and whatever your parent variable is called….. tada!

Just thought I would give you that tid-bit just incase you didn't know:)

Cheers,
Jason
August 2, 2005 21:36
 

jamie.thomson said:

Hi Jason,
Yeah, parent package configurations are very useful. I alluded to them above when I talked about the analogy of passing byval rather than byref.

Glad you sorted this out anyways :)

-Jamie
August 2, 2005 22:19
 

Jamie Thomson - Life, the universe and SSIS! said:

In SQL Server Integration Services (SSIS) variables have scope (this was not the case in the forerunner...
September 1, 2005 10:14
 

Jayson Speer said:

Hi Jamie

I am obviously a relative late comer to SSIS, but I am having the EXACT same problem as described above with passing of variables between parent & child packages.

This seems to be one area in which SSIS is a big step backwards when compared to DTS.

Try as I might, I cannot seem to pass a variable ByRef from a parent to a child package such that the parent package can 'see' whatever updates have been made by the child package.

Is it possible to post a sample of how this works? This is driving me NUTS :-(
February 9, 2006 03:41
 

Ronan said:

Hi,

How can i set a parent Package variable on a child package using the script task???


Thanks
February 27, 2006 08:45
 

jamie.thomson said:

February 27, 2006 09:29
 

SSIS Junkie said:

I have just been watching Joy Mundy's webcast " Loading a Kimball method data warehouse using SQL Server

January 16, 2007 18:00
 

SSIS Junkie said:

In SQL Server Integration Services (SSIS) variables have scope (this was not the case in the forerunner

January 16, 2007 18:15
 

Subhash Subramanyam said:

Addressing the blog below by Jason,

Would you please provide few steps how I can assign the variable values (Scope:Package) to the properties of Connection Managers, and Few Tasks using Package Configurations.

I couldn't find solution in clear steps even after googling and going through lots of blogs and related links for using package configurations.

Many thanks for your help...

-------------------------------------

jason said:

Hello again Jamie,

I just wanted to drop you a line and let you know I found another way to pass variables without setting in script objects, it has been staring me in the face this whole time. go to package configuration organizer -> add new package config, set the config type: "parent package variable" and whatever your parent variable is called….. tada!

Just thought I would give you that tid-bit just incase you didn't know:)

Cheers,

Jason

August 2, 2005 9:36 PM

March 15, 2007 16:06
 

Telmo Teixeira said:

Hi, I have a project that is using SSIS for the ETL process. Since it was installed SP2 on SQL Server 2005, the ETL process begun to work wrong. At first i don't realize what it was, then, after several tests i notice that the SSIS with SP2 does not work well when we have a Package that calls Child Packages in parallel and then those Child's Packages call Task Packages to execute a task. All of those 3 tier packages have variables with the same name, and those variables are passed from the First Package to the Child's Packages and from the Child's to the Task Packages.

What happens is that (sometimes) the Task Packages get the wrong variable (Task_1 receives the variable that is passed to Task_2)

****************************************

             Correct

****************************************

Package:                Father

variables:        A_1           B_2

Child Packages: Child_A         Child_B

variables:        A_1           B_2

Task Packages: Task_1 Task_2            

****************************************

             Incorrect (1)

****************************************

Package:                Father

variables:        A_1           B_2

Child Packages: Child_A         Child_B

variables:        A_1           A_1

Task Packages: Task_1 Task_2

****************************************

             Incorrect (2)

****************************************

Package:                Father

variables:        A_1           B_2

Child Packages: Child_A         Child_B

variables:        B_2           B_2

Task Packages: Task_1 Task_2

This situation work well in SQL Server 2005 SSIS

- RTM

- SP1

This situation work wrong in SQL Server 2005 SSIS

- SP2

Can anyone notice this situation yet?

Regards,

Telmo Teixeira

July 20, 2007 10:50
 

Langston said:

Hi all,

Here's another gotcha to look out for when using parent package configurations. I spent a few hours today dealing with this. I finally solved it, but I wish I had of found this feedback article first...sometimes google is not your friend  :-(

Parent Package Configurations Fail to load:

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=174510

September 7, 2007 22:23
 

gautam beri said:

hello,

i have a question regarding that.  In my parent package, i get data, i clean it , and at the end i save it in the result set, and now i have to access that result set from my child package, i have defined all the variables, but i just dont knw how to access that result set in parent package from child

January 31, 2008 13:31
 

jamie.thomson said:

January 31, 2008 15:30
 

gautam beri said:

hey i am getting these errors if u have an idea please let me know:

The ProcessInput method on component "Result_Set" (240) failed with error code 0xC020207D. The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.

Error: Error writing to the ADODB.Recordset object.

January 31, 2008 20:42
 

Sanjeev said:

Hi,

I wanted to do this without using Script  task. My aim is to assign the System:ErrorCode value into one of my loal variables without using script tag.

please help me.

sanjay

sanjay.bollina@gmail.com

February 6, 2008 11:47
 

Sanjeev said:

I used to Execute SQL task to do this, but i need to use some connection manager. I used ADO.net, But I wanted to use OLEDB as i am having all connections of type OLEDB. what to do regarding assingiing system avriable values to local variables.

please advice me'

sanjay.bollina@gmail.com

February 6, 2008 11:50
 

jamie.thomson said:

Samjay,

Do you want to persist the value of System:ErrorCode into a user variable (I persume when you say 'local variable' you mean 'user variable') or do you want the variable to always show the same as in System:ErrorCode ?

I don't email people. If you have questions then please ask them here or on the SSIS forum.

-Jamie

February 6, 2008 23:51
 

Wizard of Oz said:

Thanks for the package configurations idea. it works like a breeze

February 13, 2008 19:57
 

Joost said:

An other option is a script task. Instead of pulling the variables from the child package, you can push them from the parent package:

Imports System

Imports System.Data

Imports System.Math

Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

   Public Sub Main()

       ' Call child package and pass through parameters / variables

       ' The child package is called with the same connection

       ' manager as the Execute Package Task would do

       Try

           ' Configure the child package

           Dim app As New Microsoft.SqlServer.Dts.Runtime.Application()

           Dim package As Package

           package = app.LoadPackage(Dts.Connections("child.dtsx").ConnectionString.ToString(), Nothing)

           package.Variables("User::variable1").Value = "some value"

           ' Execute package and return result

           Dim result As DTSExecResult

           result = package.Execute()

           Dts.TaskResult = result

       Catch ex As Exception

           Dts.TaskResult = Dts.Results.Failure

       End Try

   End Sub

End Class

- Joost (Atos Origin)

February 19, 2008 17:14
 

VinGus said:

Hi Jamie,

Got a bizarre problem.

Have PackageA which has a connection called "Metadata" and a variable called "LogDate" which is got from a ForEach loop ADO Enumerator. I want to pass this to PackageB which has the same names as in Package A.

So in PackageB I create a parent package configuration which assigns both "Metadata" & "LogDate"

Name: MetadataConnectionString

Type: Parent Package Variable

Parent Package Variable Name: \PackageA.Connections[Metadata].Properties[ConnectionString]

Target Property: \Package.Connections[Metadata].Properties[ConnectionString]

Name: LogDateValue

Type: Parent Package Variable

Parent Package Variable Name: \PackageA.Variables[User::LogDate].Properties[Value]

Target Property: \Package.Variables[User::LogDate].Properties[Value]

When I deploy the package to MSDB and run them either from desktop or from SQL Agent, the variable in PackageB jsut doesn't get assigned.

Any ideas? What am I doing wrong?

Note - the variables in both packages are defaulted to a value and tha tis what is showing up in runtime.

Regards

VinGus

March 18, 2008 03:01
 

jamie.thomson said:

Sorry Vin. I dunno.

If you could send me a repro then I could maybe have a look.

-Jamie

March 18, 2008 03:09
 

VinGus said:

Am currently debugging this and found out this error:

---

Information: 0x40016042 at Thread: The package is attempting to configure from the parent variable "\PackageA.Connections[Metadata].Properties[ConnectionString]".

Warning: 0x8001201C at Thread: Configuration from a parent variable "\PackageA.Connections[Metadata].Properties[ConnectionString]" did not occur because there was no parent variable. Error code: 0xC0010001.

Information: 0x40016042 at Thread: The package is attempting to configure from the parent variable "\PackageA.Connections[Metadata].Properties[ConnectionString]".

Warning: 0x8001201C at Thread: Configuration from a parent variable "\PackageA.Connections[Metadata].Properties[ConnectionString]" did not occur because there was no parent variable. Error code: 0xC0010001.

SSIS breakpoint 'Break when the container receives the OnPreExecute event' at executable 'NextJobCheck' has been hit

---

Does it imply that I can't directly pass a connection. Do I've to put it in a variable?

March 18, 2008 03:21
 

jamie.thomson said:

VinGus,

Sorry, yeah, I should have spotted that before. You need to reference a variable in the parent, you can't reference a property.

Doh.

-Jamie

March 18, 2008 03:35
 

Adnan said:

Can the parent package see changes that a child package makes?

E.g. I have a parent package that has a variable called NbrLignes. The child package also has this variable, with the configuration set up to get the value from the parent. The child package writes to this variable using the RowCount transform.

Now, from the parent, I want to see this value and store it. I have tried, but I am seeing nothing at the parent level.  I.e I can't see the change that the child package made.

May 23, 2008 16:31
 

jamie.thomson said:

Adnan,

This won't work. The rowcount component will change the variable in the child package.

-jamie

May 24, 2008 08:43
 

Madhavan.TR said:

Jamie,

I have a scenario to execute child packages from a Parent Package. I try to call these child Packages from a Parent Package using For Each Loop Container  and Exectue Package Task.

Each child package has its own variables.

I have configured the child pacakge variable to the Parent Pacakge Variables using Package Configuration --> Parent Package Variable.

The problem that i am facing is I could not able to get the value from the Parent Package.

When i try to get the value in the child package, the script task says "Warning: Configuration from a parent variable "PackageName" did not occur because there was no parent variable. Error code: 0xC001F032. "

Please help me to resolve this.

Regards,

Madhavan.TR

June 2, 2008 07:33
 

jamie.thomson said:

Madhavan,

The error tells you what the problem is. I can't give you any more information other than what the error message says.

-Jamie

June 2, 2008 14:34
 

jamie.thomson said:

pardon me. Warning message, not error message.

June 2, 2008 14:34
 

bfilppu said:

I have had problems passing variables down more than 1 package.  For example, I can go from Parent to Child no problem.  But I cannot seem to get the Parent variable to Child, then to GrandChild.  It seems that I can only go 1 level deep and it does not appear to work beyond that.  Is this correct?

July 22, 2008 18:48
 

jamie.thomson said:

Brian,

How are yuo "passing" these variables?

-Jamie

July 23, 2008 11:26
 

Brian Filppu said:

using parent package configuration that gets the value of a variable.  I have variable called LogID.

Package 1, starts, creates logID 5.  Package 1 calls package 2, Package 2 gets LogID 5.  Package 2 does some work and calls Package 3.  Package 3 attempts to get the variable LogID from Package2, but it does not work.  I have done some research on this and I read that it has something do with the variable being the same name in all packages.  Package 3 with a variable of LogID cannot get the LogID from Package 2 for some reason.  

August 8, 2008 19:10

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems