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.
In this article, we 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 as Transformation demo purpose, it is designed for doing robust work. 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 SQL table and the data that we are going to use.
Configuring SSIS Script Component as Transformation
STEP 1: Drag and drop the Data Flow Task from the toolbox to control flow region, and rename it as the SSIS Script Component as Transformation.
Double click on the SSIS data flow task will open the data flow tab.
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 [SSIS Tutorials] Database as a source database and [MyEmployees] table 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 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 opened.
Here we want to demonstrate the SSIS script component as a transformation. So, we are selecting the Transformation option
STEP 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 that 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 that you want to use in the Script, and they may be user-defined variables or System default variables. Remember, variables selected as ReadOnlyVariables used for Read-only purpose (we can’t alter them)
- ReadWriteVariables: Please select the variables you want to use in the Script. Remember, variables selected as ReadWriteVariables can alter according to our requirement
STEP 6: Within the SSIS Script Component as Transformation Input Columns tab, you can cross-check the input columns.
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 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
STEP 9: Add your custom C# code here. For this example, we are concatenating First name, Last Name, and then we are converting the First Letter to Uppercase.
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: Once you finished 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 that you already created. Here we are selecting the [SSIS Script Component as Transformation] table present in the [SSIS Tutorials] Database.
STEP 12: Click on the Mappings tab to check whether the source columns 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.
From the above screenshot, you can observe that our SSIS Script Component as Transformation Package has executed successfully. Let’s open the SQL Server Management Studio and write the following query to view the data
USE [SSIS Tutorials] GO SELECT [FirstName] ,[LastName] ,[FullName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [SSIS Script Component as Transformation]
OUTPUT