In this article, we will show you, How to return Full row set (or Complete Table) from the SQL server using the Execute SQL Task with an example. This will also demonstrate the Result Set option Full Row Set. For this, we are going to use the Employee table present in the SQL Tutorial database
Before we start demonstrating the Execute SQL Task in SSIS Full Row Set, Let me show you the data that we are going to use for this example. This is Employee Table present in SQL Server that we are going to use for this example:
Execute SQL Task in SSIS Full Row Set
In this example, we will use the Execute SQL Task to select the data present in the Employee table and save the returned result set in another table. In order to do so, Drag and drop the Execute SQL Task from SSIS toolbox to designer space.
Before we start configuring the Execute SQL Task, let me create variables that are required to hold the return records. And to do so, right-click on the design area will open the context menu. Please select the Variables option from the context menu as we shown below.
As you can see from the below screenshot, we created 8 variables. Here, ResultSet variable of object type will hold the returned result set, and remaining seven column will be used in another task.
Now, Double click on the Execute SQL Task Editor will open the Editor to configure it. Let me select the Connection Type as OLE DB Connection, which is connecting to SQL Tutorial database. Next, we are using the Direct Input as the SQL statement so, click the … button to write the custom SQL command.
-- SSIS Execute SQL Task Full Row Set Example SELECT [FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] ,[HireDate] FROM [Employee]
Please change the ResultSet property value from NONE (default) to the Full result set.
Next, go to the Result Set tab, and click add button to assign the variables for the return set.
As we said before, select statement will return a complete table so we are assigning object variable (ResultSet variable) as the result variable.
Until now we successfully configure the execute SQL task. But, our intention is to save the result set in SQL database. To do so, we need a For Each Loop container. So, let me Drag and drop the Foreach Loop Container into the Control Flow region as shown below
Double click on it will open the Foreach Loop Editor to configure it. Please navigate yourself to the Collection tab, and select the Enumerator as the Foreach ADO Enumerator. Next, select the Object variable (ResultSet) as the ADO Object source. We will do one separate article for the Foreach ADO Enumerator so blindly follow the steps.
Next, we have to map the variables so navigate yourself to Variable Mappings tab.
Our Result Set will return seven column from Employee table so, we are assigning the previously created user variables to those seven columns.
Click OK to close the Foreach Loop container. Next, Drag and Drop the Data Flow task inside the Foreach Loop.
Double click on the Data Flow Task will open the data flow tab. Drag and drop OLE DB Source, Derived Column Transformation, and OLE DB Destination from the toolbox to data flow region
Double click on OLE DB source, and connect to the SQL Tutorial database from the SQL Server. Next, we selected the SQL Command as the Data access mode, and write a simple command. This command returns RowNumber as Column, and its value is 1.
SQL Command that we used in the above screenshot is:
SELECT 1 AS RowNumber
Next, Double click or right click on the Derived Column Transformation to add new columns based on the variables. First, let me add a new column for the FirstName variable.
As you can from the below screenshot, we are using the Type Cast function DT_STR to convert the variable value to string type of length 50.
Please add the new columns for the remaining variables as well.
Next, Double click on the OLE DB Destination to save these newly created columns into the SQL Server database.
For now, we are connecting to SQL Tutorial database. I forgot to create a table in the SQL Server so, let me click the New button so that OLE DB Destination editor will automatically create a table for me.
This is the table that was generated by the Ole DB. Remember, we removed the unwanted RowNumber column from this definition.
Click on the Mappings tab to check whether the source columns are exactly mapped to the destination columns.
Clicking OK to finish configuring Execute SQL Task in SSIS Full Row Set package. Let us run the package and see whether we transferred the data using Execute SQL Task or not
Let us open the SQL Server Management Studio Query window to Preview the data.
Thank You for Visiting Our Blog