posted on Thursday, January 12, 2006 12:02 AM by bknight

How to use a Script Transformation

Using a Script transformation is a handy way to extend what transforms are available to you. For example, picture social security data that goes into your Script transformation unencrypted and then out encrypted. Typically, you can also use this type of custom script to extend what a Derived Column task can do. If you have the choice between the two though, always choose a Derived Column.

A good extension of a Derived Column transformation into a Script transformation would be where you need to replace certain text with other text. If you have only two options, you could use the REPLACE expression inside a Derived Column transform. When you go above two, the Replace expression becomes less elegant. So, in comes the Script transform.

After connecting the Script transform to the data flow, go to the Input Column task for the transform and check the columns that will be sent into the script as an input. Next, go to the Inputs and Outputs tab and click Add Output. Name the columns that you wish to output. I’m going to call this one OutputValue for this example.  Next, click Design Script.

In this script you can see that first we localize the variable. Because I can’t predict whether the user inputted mixed, lower or upper case, I use the UCASE function to upper case the input value for comparison purposes. The word InputValue in Row.InputValue would be replaced with whatever you checked in the input screen. Then, the main thing to remember is Row.OutputValue will set the OutputValue output we set earlier will be set to the new value. So this code will take the input of whatever the color is and translate it to the hex value. Then, the OutputValue output will be presented to the pipeline and can be consumed by the destination or the next transform in the pipeline.

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

Public Class ScriptMain
Inherits UserComponent

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

Dim HexValue As String

Select Case UCase(Row.InputValue)
   
Case "RED"
      HexValue = "#FF0000"
   Case "YELLOW"
      HexValue = "#FFFF00"
   Case "GREEN"
      
HexValue = "#008000"
End Select

Row.OutputValue = HexValue
End Sub
End
Class 

-- Brian Knight 

Comments

# Script Transform Component to Cleanse Data

Friday, January 13, 2006 5:52 AM by Brian Knight, SQL Server MVP
In yesterday's post, I showed you how to cleanse data using the Script Component acting as a transform....

# Script Transform Component to Cleanse Data

Monday, January 23, 2006 5:26 AM by Brian Knight, SQL Server MVP
In yesterday's post, I showed you how to cleanse data using the Script Component acting as a transform....

# re: How to use a Script Transformation

Tuesday, December 18, 2007 5:35 PM by sandeep hegde
Brian,
I am using the script transformation task to call a stored proc.

My requirement is like this:
1. I have txt file containing 100... records and for each record I need to call a SP.
2. I use a data flow task and add a derived column and then use a script transformation to call the SP.
3. Inside the script try catch block I want to set a global variable to TRUE / FALSE for error handling. If i pass the GV as a read/writ evariable I get an error saying :
"The collection of variables locked for read and write access is not available outside of PostExecute."

4. Any thoughts on how to use a GV inside the script function: Input0_ProcessInputRow ????

Your suggestions will be most valuable to me:)

Thanks
Sandeep
sandeep.p.hegde@gmail.com

# re: How to use a Script Transformation

Thursday, May 08, 2008 7:10 AM by Priya
hi Brian

i need help in copying dat from text from sql table.
i have a flat file with comma seperated value and its data type is string [DT_STR][50](as shown in the flat file data source) and i am trying to copy it to the SQL table of data type nvarchar(256).
i have used the data conversion task and converted it from DT_STR(50) to DT_WSTR(256).
but this is giving me error as

Error: 0xC02020A1 at Data Flow Task, Flat File Source [1]: Data conversion failed. The data conversion for column "XXX" returned status value 4 and status text "Text was truncated or one or more characters had no match in the target code page.".
Error: 0xC020902A at Data Flow Task, Flat File Source [1]: The "output column "XXX" (130)" failed because truncation occurred, and the truncation row disposition on "output column "world_watch_text" (130)" specifies failure on truncation. A truncation error occurred on the specified object of the specified component.



can you please help me to solve this

thank you
priya
m.priyahere@gmail.com