The Script Component is one of the most important and powerful items in SQL Server Integration Services. We can use this SSIS script component as a Source, Destination, and Transformation.
This article will show you how to use the SSIS Script Component as a Transformation with a practical example. Though we are using a simple example for the SSIS Script Component for Transformation demo purposes, it is designed for doing robust work.
For example, work that is not possible for the built-in transformations, or we can use this SSIS Script Component as a Transformation to combine the work of multiple transformations in one place.
You can also look into the following links:
Before we start creating the SSIS Script Component as a Transformation package, Let us see the table and the data we will use.
Configuring SSIS Script Component as Transformation
STEP 1: Drag and drop the Data Flow Task from the toolbox to the control flow region, and rename it as the Script Component as Transformation.
Double click on the data flow task to open the data flow tab. For more Transformations >> Click Here.
STEP 2: Drag and drop OLE DB Source and double click on it will open the OLE DB Connection Manager settings
From the below screenshot, you can observe that we selected the following Database as a source database and [MyEmployees] as the source table
STEP 3: Click on the columns tab to verify the columns. In this tab, we can uncheck the unwanted columns also.
STEP 4: Drag and drop the Script Component in the SSIS toolbox to the data flow region. Once you drop the Script component, a new pop-up window called Select Script Content Type opens.
Here, we want to demonstrate the SSIS script component as a transformation. So, we are selecting the Transformation option.
S.TEP 5: Double click on the Script component will open the following editor to configure the properties. Though there are many properties, we will explore a few important features we use in our daily coding.
- Name: Please provide the Unique Name
- Description: Briefly describe the Script Functionality. It is always a good practice to provide a valid description.
- ReadOnlyVariables: Please select the variables you want to use in the Script, which may be user-defined for System default variables. Remember, variables selected as ReadOnlyVariables are used for Read-only purposes (we can’t alter them)
- ReadWriteVariables: Please select the variables you want to use in the Script. Remember, variables selected as ReadWriteVariables will alter according to our requirements.
STEP 6: You can cross-check the input columns within the SSIS Script Component as Transformation Input Columns tab.
STEP 7: Within the Input and Outputs tab, Go to Output Columns, and under output columns, we are going add one column called FullName using the Add Column button
Here, we added the FullName Column and then changed the Data type from Integer (default) to string [DT_STR]
STEP 8: Within the Script tab, please click on the Edit Script.. button to write the actual C# Script
Once you click on the Edit Script, it will open the main.cs class file to write the C# code. Please write your custom code inside the Input0_ProcessInputRow(Input0Buffer Row) function.
S.TEP 9: Add your custom C# code here. For this example, we are concatenating the First name, Last Name, and then converting the First Letter to Uppercase.
The code that we used in the above SSIS Script Component as Transformation screenshot is:
C# CODE
public override void Input0_ProcessInputRow(Input0Buffer Row) { /* * Add your code here */ string name = Row.FirstName + " " + Row.LastName; Row.FullName = FirstLetterUppercase(name); } // Function to Convert the First Character to UpperCase private string FirstLetterUppercase(string name) { // Check whether String is empty. if (string.IsNullOrEmpty(name)) { return string.Empty; } // Converting First Character to Upper Case. return char.ToUpper(name[0]) + name.Substring(1); }
STEP 10: After editing the Script, Please close the main.cs file. Next, drag the OLE DB Destinations and join the script component Output Arrow to this new OLE DB Destination.
STEP 11: Double click on OLE DB Destination and select the OLE DB Connection manager you already created. Here, we select the SSIS Script Component as Transformation table in the Database.
STEP 12: Click on the Mappings tab to check whether the source columns are mapped to the destination columns.
Click OK to finish creating our Package.
STEP 13: Right-click on the SSIS Script Component as Transformation Package in the Solution Explorer and select Execute Package.
The above screenshot shows that our SSIS Script Component as Transformation Package was executed successfully. Let’s open the Management Studio and write the following query to view the data.
SELECT [FirstName] ,[LastName] ,[FullName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [SSIS Script Component as Transformation]
OUTPUT
Comments are closed.