In this article, we will show you, How to return single row set (or Single Row) from the SQL tables using the Execute SQL Task in SSIS with an example. This will also demonstrate the Result Set option Single Rowset. For this, 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 data that we are going to use for this example. This is the Employee Table in SQL Server, and we are going to use the first row for this example:
Execute SQL Task in SSIS Single Rowset
In this example, we will use the Execute SQL Task to select the first row from Employee table, and display the returned single row set in message box In order to do so, Drag and drop the Execute SQL Task from SSIS toolbox to designer space.
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 SQL Tutorial database. Next, we are using the Direct Input as the SQL statement so, click the … button to write the custom SQL command.
-- Execute SQL Task in SSIS Single Rowset Example SELECT TOP 1 [FirstName] ,[LastName] ,[Education] ,[Occupation] ,[Sales] ,[HireDate] FROM [Employee]
Please change the ResultSet from NONE (default) to Single row, because our select statement will return one row.
I think I forgot to create variables that are required to hold the return records. So, let me close the Execute SQL Task editor, and right-click on the design are will open the context menu. Please select the Variables option as we shown below.
As you can from the below screenshot, we created 7 variables to hold seven column that is returned by the select statement.
Next, go to the Result Set tab in Execute SQL Task Editor, and click add button to assign the variables for the return set.
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.
We are done with configuring SSIS Execute SQL Task. Let me display the returned result in the Message Box. To do so, drag and drop the Script Task from SSIS toolbox into the Control Flow region, and add the Execute SQL Task output to the Script Task.
Double click on the Script task will open the following editor to configure the Script task components. We already explained the Script task configuration steps in our previous article so please refer the same. Here we are selecting all the user-defined variables (a variable that we created earlier) as Read and Write variable.
Once you selected the required variable, please click on the Edit Script.. button to write the actual C# or VB Script
Here we first declared the local variables to hold the user-defined variables that we are getting from 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);
Click OK to finish configuring the Execute SQL Task in SSIS Single Rowset package. Let us Run the package
Click the OK button on the message box to finish the execution process
Thank You for Visiting Our Blog