Execute SQL Task in SSIS Single Rowset

How to return a single rowset (or Single Row) from the SQL tables using the Execute SQL Task in SSIS with an example. It also demonstrates the Result Set option Single Rowset. For this SSIS Execute SQL Task Single Rowset demo, we are going to use the Employee table present in the SQL Tutorial database

Before we start working with the Execute SQL Task in SSIS Single Rowset, Let me show you the Employee Table in SQL Server that we are going to use for this example:

Execute SQL Task in SSIS Single Rowset 0

Execute SQL Task in SSIS Single Rowset

In this example, we will use the Execute SQL Task to select the first row from the Employee table and display the returned single row set in the message box. To do so, Drag and drop the Execute SQL Task from SSIS toolbox to designer space.

Execute SQL Task in SSIS Single Rowset 1

Double click on it will open the Execute SQL Task Editor to configure it. Let me select the Connection Type as OLE DB Connection, which is connecting to the SQL Tutorial database. Next, we are using the Direct Input as the SQL statement, so click the ….

Execute SQL Task in SSIS Single Rowset 2

Please write your custom SQL statement here. As you can from the below screenshot, we are writing a SQL Select statement with the TOP Clause to select the first row from the table.

-- Execute SQL Task in SSIS Single Rowset Example
SELECT TOP 1 [FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[Sales]
      ,[HireDate]
  FROM [Employee]
Execute SQL Task in SSIS Single Rowset 3

Please change the ResultSet from NONE (default) to Single row, because our select statement will return one row.

Execute SQL Task in SSIS Single Rowset 4

Let us create variables required to hold the return records. First, close the Execute SQL Task editor, and right-click on the design will open the context menu. Please select the Variables option.

Execute SQL Task in SSIS Single Rowset 5

As you can from the below screenshot, we created 7 variables to hold seven columns returned by the select statement.

Execute SQL Task in SSIS Single Rowset 6

Next, go to the Result Set tab in Execute SQL Task Editor and click the Add button to assign the variables for the return set.

Execute SQL Task in SSIS Single Rowset 7

As you can see from the below screenshot, we are assigning the previously created variables to all the columns that are returned by the SQL statement.

Execute SQL Task in SSIS Single Rowset 8

We completed configuring SSIS Execute SQL Task Single Rowset. Let me display the returned result in the Message Box. To do so, drag and drop the Script Task from the SSIS toolbox into the Control Flow region and add the Execute SQL Task output to the Script Task.

Execute SQL Task in SSIS Single Rowset 9

Double click on the Script task will open the following editor to configure the Script task components. Here we are selecting all the user-defined variables (a variable that we created earlier) as Read and Write variable.

Execute SQL Task in SSIS Single Rowset 10

Once you selected the required variable, please click on the Edit Script.. button to write the actual C# or VB Script

Execute SQL Task in SSIS Single Rowset 11

Here we first declared the local variables to hold the user-defined variables that we are getting from the SSIS package. Next, we are concatenating the value with custom text (“Name:” etc)

C# code we used in the below screenshot is:

-- Execute SQL Task in SSIS Single Rowset Example
String FullName = "Name: " + Dts.Variables["FirstName"].Value.ToString() + " " + 
                             Dts.Variables["LastName"].Value.ToString();
String Occupation = "Occupation: " + Dts.Variables["Occupation"].Value.ToString();
String Education = "Education: " + Dts.Variables["Education"].Value.ToString();
String Sales = "Sales: " + Dts.Variables["Sales"].Value.ToString();
String HireDate = "HireDate: " + Dts.Variables["HireDate"].Value.ToString();

MessageBox.Show(FullName + Environment.NewLine + Education + Environment.NewLine +
                 Occupation + Environment.NewLine + Sales + Environment.NewLine + HireDate);
Execute SQL Task in SSIS Single Rowset 12

Click OK to finish configuring the Execute SQL Task in SSIS Single Rowset package. Let us Run the package

Execute SQL Task in SSIS Single Rowset 13

Click the OK button on the message box to finish the execution process

Execute SQL Task in SSIS Single Rowset 14