Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

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.

Comments

Posted by Jamie Thomson on 21 September 2010

Patrick,

Apologies for appearing to be pedantic and condascending (its a disease I'm afraid :), but the Script Task exists in the Control Flow, what you're referring to is a Script Component.

I know it won't matter to most but I feel its important to use the correct terminology.

-Jamie

Posted by Patrick LeBlanc on 21 September 2010

Jamie, no apologies need, a definite oversite on my part.  Thanks for the correction.

Posted by T Murdock on 28 September 2010

Jamie, you spelled condescending incorrectly. Also, you used "its" where you meant to use "it's". I know it won't matter to most, but....

~Troy

Leave a Comment

Please register or log in to leave a comment.