-->APPLIES TO: SQL Server SSIS Integration Runtime in Azure Data Factory Azure Synapse Analytics (SQL DW)![Write Write](/uploads/1/2/5/7/125744141/318438026.png)
![Update table in ssis script task to write word Update table in ssis script task to write word](/uploads/1/2/5/7/125744141/101251473.png)
The SSIS Script Task gives an option to implement functions that are not available or possible in the SSIS toolbox (both in built-in Tasks and transformations). The SSIS script task utilizes the Microsoft VSTA (Visual Studio Tools for Applications) as the code environment in which you can write the C# or VB Script.
You use a destination component in the data flow of an Integration Services package to save data received from upstream sources and transformations to a data source. Ordinarily the destination component connects to the data source through an existing connection manager.
For an overview of the Script component, see Extending the Data Flow with the Script Component.
The Script component and the infrastructure code that it generates for you simplify significantly the process of developing a custom data flow component. However, to understand how the Script component works, you may find it useful to read through the steps for developing a custom data flow components in the Developing a Custom Data Flow Component section, and especially Developing a Custom Destination Component.
Getting Started with a Destination Component
When you add a Script component to the Data Flow tab of SSIS Designer, the Select Script Component Type dialog box opens and prompts you to select a Source, Destination, or Transformation script. In this dialog box, select Destination.
Next, connect the output of a transformation to the destination component in SSIS Designer. For testing, you can connect a source directly to a destination without any transformations.
Configuring a Destination Component in Metadata-Design Mode
After you select the option to create a destination component, you configure the component by using the Script Transformation Editor. For more information, see Configuring the Script Component in the Script Component Editor.
To select the script language that the Script destination will use, you set the ScriptLanguage property on the Script page of the Script Transformation Editor dialog box.
Note
To set the default scripting language for the Script component, use the Scripting language option on the General page of the Options dialog box. For more information, see General Page.
A data flow destination component has one input and no outputs. Configuring the input for the component is one of the steps that you must complete in metadata design mode, by using the Script Transformation Editor, before you write your custom script.
Adding Connection Managers
Ordinarily a destination component uses an existing connection manager to connect to the data source to which it saves data from the data flow. On the Connection Managers page of the Script Transformation Editor, click Add to add the appropriate connection manager.
However, a connection manager is just a convenient unit that encapsulates and stores the information that is required to connect to a data source of a particular type. You must write your own custom code to load or save your data, and possibly to open and close the connection to the data source.
![Write Write](/uploads/1/2/5/7/125744141/318438026.png)
For general information about how to use connection managers with the Script component, see Connecting to Data Sources in the Script Component.
For more information about the Connection Managers page of the Script Transformation Editor, see Script Transformation Editor (Connection Managers Page).
Configuring Inputs and Input Columns
A destination component has one input and no outputs.
On the Input Columns page of the Script Transformation Editor, the column list shows the available columns from the output of the upstream component in the data flow. Select the columns that you want to save.
![Update table in ssis script task to write word Update table in ssis script task to write word](/uploads/1/2/5/7/125744141/101251473.png)
For more information about the Input Columns page of the Script Transformation Editor, see Script Transformation Editor (Input Columns Page).
The Inputs and Outputs page of the Script Transformation Editor shows a single input, which you can rename. You will refer to the input by its name in your script by using the accessor property created in the auto-generated code.
For more information about the Inputs and Outputs page of the Script Transformation Editor, see Script Transformation Editor (Inputs and Outputs Page).
Adding Variables
If you want to use existing variables in your script, you can add them in the ReadOnlyVariables and ReadWriteVariables property fields on the Script page of the Script Transformation Editor.
When you add multiple variables in the property fields, separate the variable names by commas. You can also select multiple variables by clicking the ellipsis (...) button next to the ReadOnlyVariables and ReadWriteVariables property fields, and then selecting the variables in the Select variables dialog box.
For general information about how to use variables with the Script component, see Using Variables in the Script Component.
For more information about the Script page of the Script Transformation Editor, see Script Transformation Editor (Script Page).
Scripting a Destination Component in Code-Design Mode
After you have configured the metadata for your component, you can write your custom script. In the Script Transformation Editor, on the Script page, click Edit Script to open the Microsoft Visual Studio Tools for Applications (VSTA) IDE where you can add your custom script. The scripting language that you use depends on whether you selected Microsoft Visual Basic or Microsoft Visual C# as the script language for the ScriptLanguage property on the Script page.
For important information that applies to all kinds of components created by using the Script component, see Coding and Debugging the Script Component.
Understanding the Auto-generated Code
When you open the VSTA IDE after you create and configuring a destination component, the editable ScriptMain class appears in the code editor with a stub for the ProcessInputRow method. The ScriptMain class is where you will write your custom code, and ProcessInputRow is the most important method in a destination component.
If you open the Project Explorer window in VSTA, you can see that the Script component has also generated read-only BufferWrapper and ComponentWrapper project items. The ScriptMain class inherits from UserComponent class in the ComponentWrapper project item.
At run time, the data flow engine invokes the ProcessInput method in the UserComponent class, which overrides the ProcessInput method of the ScriptComponent parent class. The ProcessInput method in turn loops through the rows in the input buffer and calls the ProcessInputRow method one time for each row.
Writing Your Custom Code
To finish creating a custom destination component, you may want to write script in the following methods available in the ScriptMain class.
- Override the AcquireConnections method to connect to the external data source. Extract the connection object, or the required connection information, from the connection manager.
- Override the PreExecute method to prepare to save the data. For example, you may want to create and configure a SqlCommand and its parameters in this method.
- Use the overridden ProcessInputRow method to copy each input row to the external data source. For example, for a SQL Server destination, you can copy the column values into the parameters of a SqlCommand and execute the command one time for each row. For a flat file destination, you can write the values for each column to a StreamWriter, separating the values by the column delimiter.
- Override the PostExecute method to disconnect from the external data source, if required, and to perform any other required cleanup.
Examples
The examples that follow demonstrate code that is required in the ScriptMain class to create a destination component.
Note
These examples use the Person.Address table in the AdventureWorks sample database and pass its first and fourth columns, the intAddressID and nvarchar(30)City columns, through the data flow. The same data is used in the source, transformation, and destination samples in this section. Additional prerequisites and assumptions are documented for each example.
ADO.NET Destination Example
This example demonstrates a destination component that uses an existing ADO.NET connection manager to save data from the data flow into a SQL Server table.
If you want to run this sample code, you must configure the package and the component as follows:
- Create an ADO.NET connection manager that uses the SqlClient provider to connect to the AdventureWorks database.
- Create a destination table by running the following Transact-SQL command in the AdventureWorks database:
- Add a new Script component to the Data Flow designer surface and configure it as a destination.
- Connect the output of an upstream source or transformation to the destination component in SSIS Designer. (You can connect a source directly to a destination without any transformations.) This output should provide data from the Person.Address table of the AdventureWorks sample database that contains at least the AddressID and City columns.
- Open the Script Transformation Editor. On the Input Columns page, select the AddressID and City input columns.
- On the Inputs and Outputs page, rename the input with a more descriptive name such as MyAddressInput.
- On the Connection Managers page, add or create the ADO.NET connection manager with a name such as MyADONETConnectionManager.
- On the Script page, click Edit Script and enter the script that follows. Then close the script development environment.
- Close the Script Transformation Editor and run the sample.
Flat File Destination Example
This example demonstrates a destination component that uses an existing Flat File connection manager to save data from the data flow to a flat file.
If you want to run this sample code, you must configure the package and the component as follows:
- Create a Flat File connection manager that connects to a destination file. The file does not have to exist; the destination component will create it. Configure the destination file as a comma-delimited file that contains the AddressID and City columns.
- Add a new Script component to the Data Flow designer surface and configure it as a destination.
- Connect the output of an upstream source or transformation to the destination component in SSIS Designer. (You can connect a source directly to a destination without any transformations.) This output should provide data from the Person.Address table of the AdventureWorks sample database, and should contain at least the AddressID and City columns.
- Open the Script Transformation Editor. On the Input Columns page, select the AddressID and City columns.
- On the Inputs and Outputs page, rename the input with a more descriptive name, such as MyAddressInput.
- On the Connection Managers page, add or create the Flat File connection manager with a descriptive name such as MyFlatFileDestConnectionManager.
- On the Script page, click Edit Script and enter the script that follows. Then close the script development environment.
- Close the Script Transformation Editor and run the sample.
See Also
Creating a Source with the Script Component
Developing a Custom Destination Component
Developing a Custom Destination Component
19 Feb 2019CPOL
SSIS Script task to copy Table Object with data from one MSSQL server to another using Transfer SQL Server
Introduction
This tip focuses on copying database tables with its data from one MS SQL Server to another SQL server which is in a different environment. You can also copy other dependent objects, primary keys, foreign keys as well, but this post covers the basic concept on how to copy table objects using “Transfer SQL Server Objects Task“.
Background
When you want to move database tables from one SQL server to another and both SQL servers are in different environments with limited permission where creating link server is also not allowed, then we can think of the below options:
- Create SSIS package for each table using
DataFlow
task and it will take care of creating table and moving data. - Use SSMS to import data with table from one server to another.
However, both options are good if you deal with 10 to 15 tables and if it is a onetime task. But what if this is a repetitive task, number of table objects are more, and
Table
list is updating time to time?Using the Code
Note: The below code works with SQL Server 2015, 2016.
“Transfer SQL Server Objects Task” is a SSIS task used for moving data from one SQL server to another SQL server, however, in case you want to move the specific list of tables directly using this task control, it is a bit difficult. So attached here is the code for using “Transfer SQL Server Objects Task“ object in Script task control to handle the list of tables dynamically and copying it to another SQL Server.
The code has been written using Visual Basic.NET.
Points of Interest
I tried this code with OLEDB and ADO connection, but it did not work, “Transfer SQL Server Objects Task” works with Server Management Objects (SMOServer) connections.
History
- 16th February, 2019: Initial version