The Script Component is the most important, and powerful component in SQL Server Integration Services. It can act as a Source, Transformation, and Destination. In this article we will show you, How to use the SSIS Script Component as Destination with practical example. Though we are using very simple example for the demonstration purpose, it is designed for doing robust work.
You can also look into following links:
Before we start creating the SSIS package, Let us see the SQL table and the data that we are going to use.
Configuring SSIS Script Component as Destination
STEP 1: Drag and drop the Data Flow Task from toolbox to control flow region, and rename it as the SSIS Script Component as Destination.
Double click on the data flow task will open the data flow tab.
From the below screenshot you can observe that, We selected [MyEmployees] table present in the [SSIS Tutorials] Database as source database as source table
STEP 3: Click on columns tab to verify the columns. In this tab we can uncheck the unwanted columns also.
STEP 4: Drag and drop Script Component from SSIS toolbox to data flow region. Once you drop the Script component, a new pop up window called Select Script Content Type will be opened as shown below. Here we want to demonstrate about the ssis script component as Destination so, we are selecting the Destination option
Next, drag and drop the OLE DB Source Output Arrow to this script component
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 few important properties 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 the 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 are 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 be altered according to our requirement
STEP 6: Within the Input Columns tab, you can cross check the input columns.
STEP 7: Within the Connection Manager tab, you can add the OLE DB, ADO, or Flat File connection manager. So that you can call the connection from the script file. For this example, we will directly provide the file path inside the C# script
STEP 8: Within the Script tab, please click on the Edit Script.. button to write the actual C# Script
TIP: You can change the language to VB.Net using ScriptLanguage property.
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 reading each and every row, and column present in the source data to the text file.
Code that we used in the above screenshot is:
// Creating two private Variables
private StreamWriter textWriter;
private string columnDelimiter = ",";
/// This method is called once, before rows begin to be processed in the data flow.
/// You can remove this method if you don't need to do anything here.
public override void PreExecute()
/* Establish the Connection before the data flow begins.
If you place this code inside the Input0_ProcessInputRow() then it will
textWriter = new StreamWriter(@"D:\FILE EXAMPLES\Write.txt", false);
/// This method is called after all the rows have passed through this component.
/// You can delete this method if you don't need to do anything here.
public override void PostExecute()
// Closing the Connection.
textWriter.Close(); // It is always advisable to close the Connection
/// This method is called once for every row that passes through the component from Input0.
/// Example of reading a value from a column in the the row:
/// string zipCode = Row.ZipCode
/// Example of writing a value to a column in the row:
/// Row.ZipCode = zipCode
public override void Input0_ProcessInputRow(Input0Buffer Row)
I suggest you read the comments as well. Once you finished editing the Script, Please close the main.cs file, and Click OK to finish creating our Package
STEP 10: Right click on the SSIS Script Component as Destination Package in the Solution Explorer, and select Execute Package.
From the above screenshot you can observe that our Package has executed successfully. Let’s open the text file and see whether the data is written or not.
Thank you for Visiting Our Blog