Keywords: SSIS | temporary table | control flow | data flow | RetainSameConnection
Abstract: This paper addresses technical challenges in creating temporary tables in SSIS control flow tasks and querying them in data flow tasks. The core solution involves setting the Connection Manager's RetainSameConnection property to True, ensuring temporary tables remain accessible throughout package execution. It provides a detailed step-by-step implementation, including stored procedure creation, task configuration, and validation handling, serving as a practical guide for SSIS developers.
In SQL Server Integration Services (SSIS) package development, using temporary tables for data staging is a common requirement. However, challenges often arise when temporary tables created in control flow tasks need to be accessed in data flow tasks, leading to errors such as table non-existence or connection validation failures. This article, based on best practices, delves into the solution and offers a systematic implementation approach.
Core Solution: Setting the RetainSameConnection Property
By default, SSIS connection managers use independent connections between tasks, which can cause temporary tables to be lost when switching tasks. Setting the RetainSameConnection property of the Connection Manager to True forces SSIS to use the same database connection throughout the package execution, thereby retaining temporary tables. This resolves issues where tables created in control flow are inaccessible in data flow.
Configuring the Connection Manager
Start by creating an OLE DB Connection Manager pointing to the target database. In the properties window, locate the RetainSameConnection item and change its value to True. This setting ensures that temporary objects, such as global temporary tables (e.g., ##tmpStateProvince), persist across the package lifecycle.
Step-by-Step Implementation of Temporary Table Usage
The following example demonstrates how to create and query temporary tables within an SSIS package.
Creating a Stored Procedure to Generate Temporary Tables
Define a stored procedure in the target database to create and populate a temporary table. For instance:
USE TargetDatabase;
GO
CREATE PROCEDURE dbo.CreateTempTable
AS
BEGIN
SET NOCOUNT ON;
IF OBJECT_ID('TempDB..##tmpStateProvince') IS NOT NULL
DROP TABLE ##tmpStateProvince;
CREATE TABLE ##tmpStateProvince (
CountryCode NVARCHAR(3) NOT NULL,
StateCode NVARCHAR(3) NOT NULL,
Name NVARCHAR(30) NOT NULL
);
INSERT INTO ##tmpStateProvince VALUES
('CA', 'AB', 'Alberta'),
('US', 'CA', 'California');
END
GO
This stored procedure checks for and drops any existing temporary table, then creates a new one and inserts sample data.
Configuring SSIS Package Tasks
In the control flow, add an Execute SQL Task to execute the stored procedure. Set its SQLSourceType to Variable, using a string variable (e.g., User::PopulateTempTable) storing the command EXEC dbo.CreateTempTable. The connection should point to the configured OLE DB Connection Manager.
Using Temporary Tables in Data Flow Tasks
Add a Data Flow Task and use an OLE DB Source component within it. Set the Data access mode to "SQL command from variable" and select another variable (e.g., User::FetchTempData) storing the query SELECT * FROM ##tmpStateProvince. Since the temporary table is created in the Execute SQL Task and RetainSameConnection is set to True, the data flow can successfully access the table.
Handling Validation Errors: Setting ValidateExternalMetadata
During design time, SSIS may attempt to validate external metadata in data flow tasks, causing errors because temporary tables do not exist before package execution. To avoid this, select the OLE DB Source component and set the ValidateExternalMetadata property to False in the properties window. This allows the package to skip table existence checks during validation and only execute queries at runtime.
Best Practices and Additional Recommendations
Beyond setting RetainSameConnection and ValidateExternalMetadata, consider using global temporary tables (prefixed with ##) for session-wide sharing; ensure the Connection Manager uses appropriate authentication; and clean up temporary tables before and after package execution to avoid residues. Other answers mention setting DelayValidation to True as a辅助 measure, but combining it with RetainSameConnection is more effective.
In summary, by properly configuring connection properties and task parameters, temporary tables can be seamlessly used in SSIS packages, enhancing data integration efficiency. It is recommended to test these settings in development environments to adapt to different SSIS versions and database scenarios.