I had to set up an SSIS package that inserted data into a log, grab that logID and pass it to all of the sub tasks so they can use that LogID.
I am new to SSIS packages and had to do a lot of reading to come up with how I was going to create an SSIS Package, Define a package variable, then pass that variable to a task. I set up a simple proof of concept to test my ideas.
First I Opened VS, and created a new Integrated Service Project. If you have a hard time finding it, it’s Project Types: Business Intelligence Project, you may need to install this from your SQL installation.

Next I Clicked on Tools->Other Windows->Variables. Making sure I have the SSIS Package I want to work with selected and in the open window I clicked on the icon for new variable. This was important because your variable scope can be pointed to a different project or at the task level if you aren’t paying attrition.
I gave it a common name, and set the Data Type to Object. This is set to object because I had an issue passing int32 values. I’ll talk about it below.

Now in my Control Flow I opened up my “toolbar” and dropped two Execute SQL tasks, I double clicked on the first one. Because this one will pass the ID to my variable I need to set the ResultSet to ‘single row’ then I am using an OLE DB connection, and put my localHost for the server.

Then I clicked on the SQL Statement ellipses and pasted in a very simple statement “select 8 as Test” this is just so I pass a value, in my real solution I had a bunch of sql code and ened it with “select IDENT_CURRENT('log.TaskRun') as TaskRunID” .

Now I have to bind “test” in my example or “TaskRunID” in my second example to the variable. I do this in the “Result Set” tab. I simply click “Add” (if you do not see add you did not set a value for “ResultSet”) For ResultName, this is where I put “Test” or “TaskRunID” for Varable Name it will look like User::<name>. Click ok and open the next Task.

For the second Execute sql task you will fill out the same information as before this time with a new SQL Statement. For simple sake I put this
Declare @temp numeric
set @temp = ?
insert into log.taskrunlog (message) values (@temp)
Here I am jut inserting my value into the database, to show I did something. Things to note is that I am declaring my temp as numeric, this was the only int style choice I could find. Also note the “?” this is used to denote variable in SSIS. In my simple example I could replace @temp with the ? and only have one line of code, but if I wanted to reuse the ? I must put it in a variable. If I had 3 parameters then each ? will refer to a different parameter.

Next lets click on the Parameter Mapping tab and click “add” from here you are going to select the variable name from the drop down user::<name> this is an input, data type is Numeric, next it very important Parameter Name is 0. If you have a list of them the next will be 1 etc. This allows the ? to work correctly.

From here you can right click and ‘execute task or f5 or click the green arrow. In my case I had to save it to the server first before I could execute my package, I can do that by file->save copy of testVarable.dtsx as…

From here I was able to create a task and execute it from sql, I’ll be showing that part next week.