Using Temp Tables in SSIS
tables are created in the TempDB database, which persists for a particular session. The
objective is to maintain that session, until the temp table information
is used and dump the data into a physical table. To maintain a session in SSIS, there is a
‘Retain Same Connection’ property of the Connection Manager. If we need to
maintain a single session, we have to mark this property ‘True’.
start with a sample package. I will create the package step by step.
Create two Connection manager’s – ‘Server 1’ and ‘Server 2’. Server 2 will be
the server where we need to create the temp table using Server 1 and thereafter use that
temp table in the queries executed on Server2.
the RetainSameConnection property of the Server 2 to True.
Execute SQL Task in the Control Flow.
the SQL Statement
create table #temptestusers (id int)inside the
SQLStatement field in the Execute SQL Task Editor. This will create the temp table. Also set the Connection to
DFT below the Execute Sql Task, which will insert the data in the temp table.
OLEDB source and OLEDB destination in the DFT
OLEDB Source, enter the query
SELECT 1 as id UNION ALL SELECT 2 as id
step is to connect the ‘OLEDB Destination’. Our objective is to load the data
into a temporary table. However, you will not be directly able to select the temp
table in Design mode. So, we will create a new temp table, if you do not have
table creation rights, you can create a global temporary table and select
that. Here, we will create a TestDestination table and map the Source and
Destination columns :
Now, as we mapped it in design mode, we can modify it
later to use the Temporary table. To use Temporary table in the Destination, follow
the below steps:
a) Go to SSIS Menu > Select Work Offline
b) Select the OLEDB Destination > Properties >
Set the OpenRowset property to use the temporary table ‘dbo.[#temptestusers]’
Now, we can verify the data in the temp table. First of
all Deselect the ‘Work Offline’ mode, which was used in above step. To verify
we will add other Data Flow Task which will query this temp table created above
and will load the data from it to another server table. Add a second DFT, ‘Use
the temp table and insert in physical table’.
Add OLEDB Source and Destination in the new DFT. Use
SQL to select the rows from temp table
select id from.
You will not be able to select it in design mode, as the
temp table does not exist right now. So, first select from any other table and
start the DFT working, then follow the same steps as shown above. ‘Go to
the SSIS menu -> Select Work Offline -> Go to the OLEDB Source Properties
-> Select SQLCommand and modify the query in the String Value editor to use
the temp table.
Next in the OLEDB Destination, select the server on which
you have permission to create table, in this case I am selecting Server1.
That’s it. The temporary table package is now completed.
We can run the package and review the flow of data.
2 Rows transferred into #temptestusers table.
2 rows transferred to the physical table from temp table.
Hopefully this will help anyone to make use of
temporary tables in their design. The only thing is to make sure of is that we set
‘Work Offline’ flag on/off in the package.