Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSIS Script Component Write to Variable

This is with SQL 2005.

I have a script component inside a Data Flow Task. I would like to read from the input columns and write the data to a global user variable.

I've set my input columns and added my global user variable as a ReadWriteVariable to the script component properties.

Here is my code, I'm just trying to alter the value of the global user variable here, but its not working. When I write out the value of the variable in another task it still has its default value:

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

Public Class ScriptMain
    Inherits UserComponent
    Dim updateSQL As String

    Public Sub Main()
        Dim vars As IDTSVariables90

        VariableDispenser.LockOneForWrite("SQL_ATTR_Update", vars)
        vars("SQL_ATTR_Update").Value = "Test"
        vars.Unlock()
    End Sub


    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
        'updateSQL = Row.ITMID + Row.PRCCAT
    End Sub

End Class

I have also tried with no luck:

Me.ReadWriteVariables("SQL_ATTR_Update").Value = "Test" 
like image 828
MaxGeek Avatar asked Feb 17 '10 21:02

MaxGeek


People also ask

How do I use variables in script task in SSIS?

You add existing variables to the ReadOnlyVariables and ReadWriteVariables lists in the Script Task Editor to make them available to the custom script. Keep in mind that variable names are case-sensitive. Within the script, you access variables of both types through the Variables property of the Dts object.

How do I write a script in SSIS?

Configuring the Script TaskProvide the custom script that the task runs. Specify the method in the VSTA project that the Integration Services runtime calls as the entry point into the Script task code. Specify the script language. Optionally, provide lists of read-only and read/write variables for use in the script.

What is the difference between script task and script component in SSIS?

The Script task is configured on the Control Flow tab of the designer and runs outside the data flow of the package. The Script component is configured on the Data Flow page of the designer and represents a source, transformation, or destination in the Data Flow task.

What is DTS in SSIS script task?

The Dts object is an instance of the ScriptObjectModel class. ScriptTask. Defines the classes for the Script task, which lets developers write custom code to perform functions that are not available in the built-in tasks provided by Integration Services.


2 Answers

I figured it out.

From MS:

In Script component code, you use typed accessor properties to access certain package features such as variables and connection managers.

The PreExecute method can access only read-only variables. The PostExecute method can access both read-only and read/write variables.

For more information about these methods, see Coding and Debugging the Script Component.

http://msdn.microsoft.com/en-us/library/ms136031.aspx

It looks like Dts is only available in Script Task.

Here is what the code looks like:

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

Public Class ScriptMain
    Inherits UserComponent
    Dim updateSQL As String


    Public Overrides Sub PostExecute()
        Me.ReadWriteVariables("SQL_ATTR_Update").Value = "Test"
    End Sub

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
        'updateSQL = Row.ITMID + Row.PRCCAT
    End Sub

End Class
like image 196
MaxGeek Avatar answered Nov 24 '22 02:11

MaxGeek


Here is how you could do it: http://microsoft-ssis.blogspot.com/2011/01/how-to-use-variables-in-script.html

like image 44
Joost Avatar answered Nov 24 '22 02:11

Joost