Wednesday, January 21, 2009

Script Task

We need to use the Script task to do any custom coding. It uses the the MS Visual Basic .NET language.

1. Drag the Script task to the Control flow window.
2. Name the task appropriately and move to Script tab.
3. You need to specify any SSIS variables that you wish to use inside the Script task.
4. The variables can be distinguished between "ReadOnly" Variables and "ReadWrite" Variables. 5. Multiple variables can be specified using a comma separator.




6. You cannot specify the same variable as both "ReadOnly" and "ReadWrite". The Script task acquires the respective locks for these variables at the beginning of the task execution and releases the locks on completion of script task execution. If it fails to acquire lock on any of the variables (even after certain retries), or fails to find the variable in the list, then the task fails.



7. To write the script, click on "Design Script". MS Visual Studio opens up in a new window.
8. It shows you the function Main. It is the function that gets called on start of execution. You can write your code here. Just ensure your Main function ends with statement



Dts.TaskResult = Dts.Results.Success



9. A lock can even be acquired on a variable through the script inside the Script task. However, the varaible should not be present in either of the lists stated above. This results in lock conflict and the Task fails.



10. Lock can be achieved by following manner through the VB.net code:



Dim var As Variables
Dts.VariableDispenser.LockOneForWrite(VariableName, var)



'Now set the value here
var(0).Value = VariableValue



'Release the lock here
var.Unlock()





11. In case, you want to lock the variable for Read, in similar way just use the function :

Dts.VariableDispenser.LockOneForRead(VariableName, var)





1 comment: