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

Welcome to blogs.conchango.com

SSIS Junkie

SSIS: Writing to a variable from a script task

This is a very short post containing a useful code snippet for writing to a SQL Server Integration Services (SSIS) variable from within a script task.

Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

  Public Sub Main()

    Dim vars As Variables

    Dts.VariableDispenser.LockOneForWrite("vMyVar", vars)

    vars(0).Value = "Hello World"

    vars.Unlock()

    Dts.TaskResult = Dts.Results.Success

  End Sub

End Class

 

The other option is to place the variable name into ReadWriteVariables property of the script task...but doing it in code is the "proper" way to do it!

-Jamie

Update: As Kirk pointed out in his comment (below) both ways will work. I just like the coding method better. It gives you a sense of control f you know what I mean, you have explicit control over what is going on and at what stage the variable gets locked!

 

Published 09 February 2005 08:45 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

 

jamie.thomson said:

Actually, either way is OK. Both correctly lock the variables etc.
February 11, 2005 22:58
 

TrackBack said:

DatabaseDaily.com
February 14, 2005 08:54
 

hitendra said:

i tring to change the connection string by using global variable value but it's not working this is actual code ,if any wrong send me the code

conn1 = Dts.Variables("gFilePath").Value.ToString

       conn2 = Dts.Variables("gServer").Value.ToString

       Dts.Connections("SourceConnectionOLEDB").ConnectionString = conn1.ToString  ' Dts.Variables("gFilePath").Value.ToString().Trim

       Dts.Connections("DestinationConnectionOLEDB").ConnectionString = conn2.ToString  'Dts.Variables("gServer").Value.ToString().Trim

February 27, 2007 15:41
 

David R. K. DeLoveh said:

Another important thing to note about doing it in the code is you can control the time the variable is locked for.  If your Script task does other work that takes a bit of time, like iterating over directories or waiting on a web service response then using the ReadWriteVariables property the variable will be locked the entire duration of the script task.  Other tasks that may be running can't access it during that time which will cause a failure after a number of retries.

May 4, 2007 16:16
 

SSIS Junkie said:

Monday 27th August 2007 will, in the future, be euphamistically referred to as "not one of my better

August 28, 2007 03:38
 

helpingyouhelpme said:

Question:  Can I read variables in the script code body without explicitly placing them in the ReadOnlyVariables comma-separated list?  I have yet to be able to not get the "does not exist in container" exception.  My SSIS acts like it will not let the script task read variables unless they are explicitly listed in the ReadOnlyVariables. Thanks for the help.

December 1, 2007 21:51
 

jamie.thomson said:

You can manually unlock them (using code) in the script task if you like. That won't solve your broblem though. If it doesn't exist then it doesn't exist. perriod.

-Jamie

December 1, 2007 21:58
 

helpingyouhelpme said:

I created the indirect config file with a simple string variable...

I created a variable in the package with same name as what is found in the indirect config file... (because i figured that SSIS would preload the variable from the config file for me)...

I set up the package to use the right configuration.

Now, in my script task, I can access the package variable but it does not have the data from the external file.  I know how to create the external file and link it to the package. My question is: how do i link the package variables to the external source?  SSIS only did this for me when I created a package, created variables, then created a config which stored itself in the sql server.  In that case, i change something in sql server config table and then the script task variable is using the correct data.  

I can't figure out what is wrong.   Thanks for the help.

December 1, 2007 22:34
 

Jeroen said:

Question:

I created a variable within my script which holds a boolean value to indicate whether the script was succesful (because I want to continue in a foreach loop even when the script within that loop fails).

Now I want to use that variable in a Precedence Constraint as an Expression to check if the flow can continue to the next task or take the next step in the foreach loop. I can't do this because after clicking the test button it says the variable does not exist.

How can I do this?

May 9, 2008 10:19
 

jamie.thomson said:

Jeroen,

It sounds OK to me. I can only imagine that you have the variable scoped incorrectly.

-Jamie

May 9, 2008 10:24
 

Jeroen said:

Jamie: Thanks for pointing me in the right direction!! Appreciate it very much!

I'm just starting to get myself familiar with the wonders of SSIS so I hadn't declared the variable at all *silly me*. Later I found that the function was still "selected" when I declared the variable so, yes.. it was scoped for that function only. Clicking on the canvas of the dtsx package and declaring the variable at dtsx level resolved this problem! Hope this post helps other beginners as well.

May 9, 2008 10:56
 

Sam said:

I'm seeing very strange behavior from some packages not being able to access variables.

I'm running a large ETL and several other packages have used identical code without a hitch.  I'm iterating over files in a folder and the script task moves them to a "completed" folder with a path specified by a variable.  Each package accesses a different variable because each package moves a file to a different location.  My code starts like this:

Dim varsHolder as Variables

Dts.Events.FireInformation(0, "FileMove", "Pulling environment vars from DTS: Setup_Col_62_Path", String.Empty, 0, False)

Dts.VariableDispenser.LockOneForRead("Setup_Col_62_Path", varsHolder)

Dim TRUSTEE_SPECIFIC_PATH As String = CStr(varsHolder("Setup_Col_62_Path").Value)

varsHolder.Unlock()

Now, here's the tricky part:  this works 90% of the time.  I'll be iterating over, say, 400 input files, and suddenly on loop 350, the package fails with:

The script threw an exception: Failed to lock variable "Setup_Col_62_Path" for read access with error 0xC0010001 "The variable cannot be found. This occurs when an attempt is made to retrieve a variable from the Variables collection on a container during execution of the package, and the variable is not there. The variable name may have changed or the variable is not being created.".

This happens randomly.  Have I discovered a bug in SSIS?  Why on earth would this work (for example) 249 times in a row, and then fail on the 250th?

June 3, 2008 20:37
 

Online pharmacy aldactone. said:

Online pharmacy aldactone.

July 30, 2008 03:13
 

Tab said:

August 22, 2008 16:05
 

Marty said:

In the old DTS you simply defined a global varial and then referenced it. SIMPLE!!

Now I can't get my global variables to update at all. I specified the variables in the ReadWriteVariables but the Dts.Variables("myVar").Value = "000000" does not update the variable. It goes back to its original value after the Script Task has completed.

WTF? How in the hell does Microsoft turn this into such a ClusterF

September 11, 2008 18:53
 

The Holon Farm ???? Save File Info via an Execute SQL Task said:

December 31, 2008 20:22

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

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