How to empty my destination table before inserting new records in SSIS?
Categories:
Efficiently Emptying Destination Tables in SSIS Before Data Insertion

Learn various methods to clear your destination table in SQL Server Integration Services (SSIS) packages before loading new data, ensuring data integrity and preventing duplicates.
When working with SQL Server Integration Services (SSIS) packages, a common requirement is to ensure that the destination table is empty before new records are inserted. This is crucial for scenarios like full data refreshes, preventing duplicate entries, or maintaining data integrity. This article explores several effective methods to achieve this within your SSIS workflows, ranging from simple SQL commands to more advanced SSIS components.
Method 1: Using an 'Execute SQL Task' for TRUNCATE TABLE
The most straightforward and often most efficient way to empty a table is by using the TRUNCATE TABLE
command. This command quickly removes all rows from a table without logging individual row deletions, making it faster and less resource-intensive than DELETE
for large tables. In SSIS, you can execute this command using an 'Execute SQL Task'.

SSIS Package Flow: Truncate Table before Data Load
1. Add an Execute SQL Task
Drag and drop an 'Execute SQL Task' from the SSIS Toolbox onto the Control Flow.
2. Configure the Task
Double-click the 'Execute SQL Task' to open its editor. In the 'General' page, set the 'Connection' to your OLE DB connection manager that points to your destination database. For 'SQLSourceType', select 'Direct input'.
3. Enter the TRUNCATE Command
In the 'SQLStatement' property, enter the TRUNCATE TABLE YourSchema.YourTableName;
command. Replace YourSchema.YourTableName
with the actual schema and name of your destination table.
4. Connect to Data Flow Task
Connect the 'Execute SQL Task' to your 'Data Flow Task' (which handles the data insertion) using a precedence constraint. Ensure the constraint is set to 'Success' so the data flow only runs after the table is truncated.
TRUNCATE TABLE [YourSchema].[YourTableName];
SQL command to truncate a table
TRUNCATE TABLE
. It cannot be rolled back and will remove all data permanently. Ensure you have proper backups or are certain about the operation.Method 2: Using a 'Data Flow Task' with an OLE DB Command (DELETE)
While TRUNCATE TABLE
is generally preferred for its speed, there might be scenarios where you need to use DELETE
(e.g., if you need to delete only specific rows, or if the table has foreign key constraints that prevent truncation without disabling them). You can execute a DELETE
statement within a 'Data Flow Task' using an 'OLE DB Command' transformation, though this is less common for full table clears.
For a full table clear, using an 'Execute SQL Task' with DELETE FROM YourSchema.YourTableName;
is still more appropriate in the Control Flow, similar to the TRUNCATE
method. The 'OLE DB Command' is typically used for row-by-row operations within the data flow.
DELETE FROM [YourSchema].[YourTableName];
SQL command to delete all rows from a table
IDENTITY
column, TRUNCATE TABLE
will reset the identity seed to its original seed value, whereas DELETE
will not. This is an important distinction for subsequent inserts.Method 3: Using a Stored Procedure
For more complex scenarios, or when you want to encapsulate the table clearing logic (e.g., handling foreign key constraints, logging, or conditional clearing), you can create a stored procedure in your database. This stored procedure can then be called from an 'Execute SQL Task' in SSIS.
CREATE PROCEDURE [dbo].[ClearDestinationTable]
AS
BEGIN
-- Disable foreign key constraints if necessary
-- EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
TRUNCATE TABLE [YourSchema].[YourTableName];
-- Re-enable foreign key constraints if necessary
-- EXEC sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
END;
Example stored procedure to clear a table
1. Create the Stored Procedure
Define and create your stored procedure in the SQL Server database that contains your destination table. This procedure should contain the TRUNCATE TABLE
or DELETE
command.
2. Add an Execute SQL Task
In your SSIS Control Flow, add an 'Execute SQL Task'.
3. Configure the Task to Call SP
Configure the 'Execute SQL Task' to use your OLE DB connection. Set 'SQLSourceType' to 'Direct input' and in the 'SQLStatement' property, enter EXEC [dbo].[ClearDestinationTable];
(or the name of your stored procedure).
4. Connect to Data Flow Task
Connect this 'Execute SQL Task' to your 'Data Flow Task' with a 'Success' precedence constraint.