![]() |
|
|
|
By: Brady Upton | Read Comments (14) | Related Tips: More > Integration Services Development |
I'm trying to use a temp table in an SSIS package. It seems like everything is working correctly until I try to query the temp table. What am I doing wrong?
Creating temp tables in SSIS seems like a straight-forward process using the Execute SQL Task, however there are a couple of properties that must be changed. In this tip, we’ll walk through creating a simple temp table in SSIS.
First, I will drag an Execute SQL Task into my Design view and rename it Create Temp Table:

Next, I will right click and edit and choose my connection and SQLStatement:
(The SQL statement used in this example is below)
IF OBJECT_ID('tempdb..##tmpTeams') IS NOT NULL
DROP TABLE ##tmpTeams
CREATE TABLE ##tmpTeams
(
Team VARCHAR(255),
Mascot VARCHAR(255),
State VARCHAR (2)
)
INSERT INTO ##tmpTeams VALUES
('Auburn', 'Tigers', 'AL'),
('Alabama', 'Crimson Tide', 'AL'),
('Mississippi', 'Rebels', 'MS'),
('Louisiana State', 'Tigers', 'LA'),
('Mississippi State', 'Bulldogs', 'MS'),
('Arkansas', 'Razorbacks', 'AR'),
('Texas A&M', 'Aggies', 'TX'),
('Georgia', 'Bulldogs', 'GA'),
('Tennessee', 'Volunteers', 'TN'),
('Vanderbilt', 'Commodores', 'TN'),
('Florida', 'Gators', 'FL'),
('South Carolina', 'Gamecocks', 'SC'),
('Missouri', 'Tigers', 'MO')
Next, I will drag a Data Flow task into my Design view, rename it Query and drag the precedence constraint between the two:

For the Data Flow task we are going to query the temp table and export the results to a database table. Right click the Data Flow task and choose Edit. Drag a OLE DB Source and a OLE DB Destination task into the Design view.

To avoid errors when configuring the OLE DB Source we need to create the temp table first using SSMS. In SSMS run the following statement to create a global temp table:
CREATE TABLE ##tmpTeams
(
Team VARCHAR(255),
Mascot VARCHAR(255),
State VARCHAR (2)
)
Once the table has been created, let’s go back into our SSIS package. Right click OLE DB Source and choose Edit. Choose your data source and choose SQL command for the Data access mode dropdown. In the SQL command text we will need to create our SQL statement:

Hit OK on the OLE DB Source window and right click OLE DB Destination and choose Edit. Choose your datasource and choose "Table or View – fast load" from the Data access mode dropdown. I already created a table called AlabamaTeams that I will use to display the data and will use this name in the dropdown for the Name of the table or the view.

Once finished configuring the OLE DB Destination hit OK. We are now ready to execute the package. Hit F5 to begin or choose Debug, Start Debugging from the Menu bar:

After executing the package, an error will occur on the Query task:

If we look at the Progress tab we can see that the reason this error occurs is because after the temp table is created in the “Create Temp Table” task it is deleted. The “Query” task searches for the table but can’t find it.

To fix this issue, we will need to change a few properties on the tasks and connection manager. The first property is a connection manager property. If you right click the OLEDB Connection Manager and choose properties you will see a property called RetainSameConnection. This must be set to “True”. RetainSameConnection means that the temp table will not be deleted when the task is completed.

The second property we must change is the Execute SQL Task property. If you right click on each task (Create Temp Table and Query) and choose properties you will see a property called DelayValidation. This must be set to “True”. DelayValidation means that the task will not check if the table exists upon creation.

Once we have set both properties to “True”, execute the package again. The package should be successful this time:

If we switch back over to SSMS and query the table, AlabamaTeams, we should return a result set:

**Note: I’m using global temp tables (##tmpTeams) instead of local temp tables (#tmpTeams) because when I create the table using SSMS the first time it uses a different session (SPID) therefore when I try to configure my OLE DB Source it cannot find the temp table and gives the following error:

With that said, only global temp tables should be used. Also, make sure when you are creating your global temp tables that the table name does not interfere with any other global temp tables that may be created using other processes.
| Friday, December 07, 2012 - 7:58:33 AM - Hilary Huber | Read The Tip |
|
Thanks for the article, it is very detailed and easy to follow. I am going to try this! |
|
| Friday, December 07, 2012 - 9:36:54 AM - Kit | Read The Tip |
|
Thanks for sharing this article. Just tried and realise how it works. |
|
| Friday, December 07, 2012 - 10:09:20 AM - David | Read The Tip |
|
Great article! |
|
| Friday, December 07, 2012 - 11:08:33 AM - Junior Galvão - MVP | Read The Tip |
|
Brady, Congratulations for the article.
I am doing to test this! Regards. |
|
| Friday, December 07, 2012 - 12:49:47 PM - Amar Sale | Read The Tip |
|
Amazing article on using Temp tables in SSIS. Helped me in preparing for my interviews. Will try it... Thank you very much !!!
Amar Sale |
|
| Saturday, December 08, 2012 - 9:14:23 AM - Sherlee | Read The Tip |
|
Congratulations! Nice article and very helpful for me because I'm currently exploring and learning how to use SSIS in my projects. Thanks for sharing this one. |
|
| Monday, December 10, 2012 - 12:16:47 AM - Rajesh Sunkariya | Read The Tip |
|
Thanks for sharing this article with us. i will try to use this facility in my next SSIS Package Development. |
|
| Tuesday, December 11, 2012 - 5:55:13 AM - Vijay Sarade | Read The Tip |
|
Thanks for sharing this, its very helpful for beginner.
|
|
| Friday, December 14, 2012 - 12:36:25 AM - Vinod | Read The Tip |
|
Good point to share and I appreciate the way it has explained. |
|
| Thursday, December 27, 2012 - 10:02:43 AM - sal | Read The Tip |
|
I've never used temp tables in SSIS but if I do I will definitely give this a go. thanks for posting. |
|
| Wednesday, January 02, 2013 - 6:07:49 PM - Duncan | Read The Tip |
|
Great piece of information , it is highly appreciated.
|
|
| Monday, January 28, 2013 - 9:51:28 PM - Prasanta Behera | Read The Tip |
|
Hi Brady, Even we can use local temp tables(#) in ssis package. After your first run with the above settings or all set with the meta data, you go to the OLEDB Source properties -> then change the property "SQL Command" to select data from local temp table (select * from #temp1) then change the CREATE and INSERT statement to local temp table.
I think, using local temp table is better than using global temp table in ssis package.
Thanks, Prasanta
|
|
| Tuesday, January 29, 2013 - 6:41:22 AM - Akshay Arora | Read The Tip |
|
I completely agree with Prasanta, we can use local temp table in SSIS. |
|
| Friday, May 10, 2013 - 12:09:24 PM - Stephen | Read The Tip |
|
I would like to use local temp tables instead of global. The problem is I still get an error because the data flow task cannot find my local temp table. Not sure why this is, I have RetainConnection set to true. And DelayValidation is also true. i.e - SQL TASK: CREATE TABLE #JobValidation ( event_id numeric(12,0) ,job_no varchar(20) ,contract int ,priority int )
SQL DESTINATION: INSERT INTO #JobValidation (event_id, job_no, contract, priority) VALUES ( ?, ?, ?, ?) NB: The parameter mapping was setup by first creating a global temp table to avoid the common metadata problem.
The error received: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E37. An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80040E37 Description: "Invalid object name '#JobValidation'.". |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |