Blog Post

SSIS: Assign a value to Variable using Dataflow Script task

,

I am not quite sure how many of you have tried to set or change the value of a variable using an SSIS script task, but if you have tried I am sure that you may have ran into a few road blocks.  Recently I tried to do this and I quickly realized that it is not as straightforward as I thought.  To do this configure your package so that you have a source of some type on the data flow design surface.  Drag a script task onto the data flow and connect it to the source.  Typically, you would set the variable you want to read from or write to as a ReadOnly or ReadWrite value on the Custom Properties of the script task.

 image

This is only the case on the Control Flow.  In the case of the data flow this is not required.  I don't claim to be a developer, but I do know how to use the Internet to find a solution to a problem.  After searching for about 10 minutes I found three posts that assisted me in solving the problem.  Each post provided a little snippet of code that added to my solution.  Here is the code:

   1:      public override void Input0_ProcessInputRow(Input0Buffer Row)
   2:      {
   3:          
   4:          IDTSVariables100 vars = null;
   5:          VariableDispenser.LockOneForWrite("intMaxSalesDetailsID", ref vars);
   6:          if(!Row.SalesDetailID_IsNull)
   7:              vars[0].Value = Row.SalesDetailID;
   8:          vars.Unlock();
   9:      }

 

On line 4 declare an interface that is going to allow us to access the variable.  In the next line we lock the variable for writing.  In line six ensure that the value that is being assigned to the variable from the Input Row is not null.  Then on the next line set the value of the variable to the desired column from the buffer.  Lastly, unlock the variable.  That's all to it.  If you have other method, preferably a simpler approach, send me an email at pleblanc@pragmaticworks.com or post it here.

Talk to you soon,

Patrick LeBlanc, SQL Server MVP, MCTS

Founder TSQLScripts.com and SQLLunch.com.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating