A Step-by-Step Guide on How to Load and Overwrite Data from SQL Server into an Excel File Using SSIS

FavBoladale
6 min readOct 24, 2023

--

Introduction

In the world of data integration and ETL (Extract, Transform, Load), SQL Server Integration Services (SSIS) is a powerful tool that allows you to connect to various data sources, transform the data, and load it into different destinations. One common destination for your data is Microsoft Excel.

Overwriting data in an Excel file using SQL Server Integration Services (SSIS) can be necessary for several reasons, depending on your data integration and reporting requirements. Some of these include periodic data updates, automated reporting, data sharing among team members, or improvement of performance and storage

In this blog post, we’ll explore how to load and overwrite data into an Excel file using SSIS, step by step.

Prerequisites:

Before we dive into the process, ensure you have the following prerequisites:

  • SQL Server Data Tools (SSDT) installed.
  • SQL Server Integration Services (SSIS) installed.
  • A basic understanding of SSIS packages.
  • SQL Server Management Studio (SSMS)

Step 1: Create and Configure a New SSIS Project

Create a new Integration Services Project. In the Solution Explorer, right-click on the “SSIS Packages” folder and select “New SSIS Package” to create a new package.

Creating and Configuring a New SSIS Project
New SSIS Package

Step 2: Add Data Flow Task

Within your SSIS project, add a Data Flow Task to your control flow by dragging and dropping a Data Flow Task from the Toolbox onto the Control Flow canvas.

Adding a Data Flow Task to your control flow

Step 3: Configure the Data Source

In the Data Flow tab, drag and drop an “OLE DB Source” as a data source component from the SSIS Toolbox onto the Data Flow canvas. Then double-click on the “OLE DB Source” to configure the source component to extract the data you want to overwrite in Excel.

Configuring the Data Source- OLE DB

You can click new to select the server name, the database you want to connect to, and the table or view.

Configuring the properties used by the data flow to obtain data from our OLE DB source

Step 4: Configure the Excel Destination

Add an Excel Destination component from the toolbox to the Data Flow canvas.

Connect the output of your data source to the Excel Destination. Double-click the Excel Destination to configure it.

Configuring the Excel Destination

In the Excel Connection Manager, select an existing Excel file or create a new one where you want to overwrite the data. In the Excel Destination, specify the worksheet where you want to write the data. You can also set the “Excel Version” property to the appropriate version if it’s not detected automatically. Click OK to close the dialog.

Configuring the properties that enable the insertion of data via Excel provider

Step 5: Configure the SQL Command or Table/View Name

In the “OLE DB Source Editor,” under the “Data access mode” section, you can either specify an SQL command or choose a table or view from the Excel file in the “Name of the table or view” dropdown.

If you’re using an SQL command, enter your query in the “SQL command text” box.

Configuring the SQL Command

Step 6: Mapping Columns

After configuring the source, go to the “Columns” tab in the “OLE DB Source Editor” to map the corresponding columns from your data source (in this case your SQL serve)r to the destination in your SSIS data flow which is the Excel file.

Mapping Columns

Step 7: Execute the Package

Save and execute the package by right-clicking on it in the Solution Explorer and selecting “Execute Package.”

If you follow the steps religiously, there is a high possibility of running into certain errors as seen in the image below:

Package Validation Error

Step 8: Error Handling and Logging

It’s essential to implement proper error handling and logging in your SSIS package. You can add error flow components and configure logging to capture any issues during the execution of the package.

This error occurred because Excel does not recognize the data type “VARCHAR” as you would find in a database system (e.g., SQL Server, MySQL) which typically represents a variable-length character string with a character set (e.g., ASCII or UTF-8). Instead, Excel uses a general format called “Text” for representing alphanumeric or string data. In Excel, text data is treated as a general text string without a specific character length limit.

To handle the error above, I will be adding “Data Conversion” to the Data Flow so as to change the columns (City and Country) data type from a string [DT_STR] to a Unicode string.

Configuring the properties used to convert the data type of an input column to a different data type.

Also, will be editing the mapping to Copy of City and Copy of Country to City and Country respectively.

Configuring the properties that enable the insertion of data via an Excel provider

Step 9: Verify the Overwritten Data

Now you can save and execute. Your package execution should be completed with success by now. Open the Excel file to verify that the data in the specified worksheet has been overwritten with the new data from the source.

Data overwritten successfully

Common Error in Overwriting Data

More often than not, a major issue arises from this type of Data Flow where the more you re-execute the data flow, the more the Excel file gets populated with duplicate data. This is how to resolve that:

Step 1:

Add “Execute SQL Task” to the control flow (as seen in the image below) to drop and recreate the Excel sheet when you execute the project. Then connect it to the Data Flow task you already have.

Adding “Execute SQL Task”

Step 2:

Double-click and edit the Connection type to Excel, Connection to Excel Connection Manager, and write an SQL code to drop the table and recreate it when you re-execute.

Configuring the properties required to run SQL statements
Configuring the properties required to run SQL statements
Drop Table MyCityData
go
CREATE TABLE `MyCityData` (
`Id` INT,
`City` NVARCHAR(100),
`Country` NVARCHAR(100)
)

Then re-execute and check your Excel file, the duplicates should be gone. Even when you re-execute it multiple times, you will not see a duplicate because of the SQL code you just included.

Conclusion

Overwriting data in an Excel file using SQL Server Integration Services (SSIS) is a common task in data integration projects. By following these steps, you can create a robust SSIS package that extracts data from a source, overwrites the data in an Excel file, and automates the process for your data integration needs. Remember to save your work regularly, test your package thoroughly, and consider scheduling its execution as needed to keep your data up-to-date.

--

--