The Secret of Using Temp Tables in a DTS Transform Task
Say you want to take one of your static tables and flip it upside down and backwards by using a temp table, then export it to some file or something using SQL Server 2000’s DTS.
I’m not sure how often this is actually useful, because the reason I have to do this is because i have a 60+ field table that we call a database that has 6 fields that really should be one 2 fields (id & data field) in an another table.
Trick #1
You can’t use a regular temp table. You have to use a global temp table. You can create these just the same as a regular temp table, but you need to use two number signs. oooh, fancy.
--Create a regular temp table
CREATE TABLE #RegularTemp (Column1 varchar(5))
--Create a global temp table
CREATE TABLE ##GlobalTemp (Column1 varchar(5))
Put all your create statements in a SQL Task and then populate the table. This also works if you’re doing a “SELECT INTO” statement.
It’s also a good idea to put a preliminary SQL task to check for the existence of the temp table in case the table already exists when the package is run. That would look something like this
--Check for temp table and delete if it exists
IF object_id('##GlobalTemp') IS NOT NULL
BEGIN
DROP TABLE ##GlobalTemp
END
WARNING: Running this from Query Analyzer won’t work, because you have to name the DB as in…. object_id(tempdb..##GlobalVariable). But that syntax won’t work in DTS, you have to NOT name the database.
Trick #2
This was the hard part to figure out. The DTS Transform task won’t populate the column names or types unless it can find the table somewhere. You can get around this by going to Query Analyzer and creating the temp table. Then return to the DTS package creator, Put in your SELECT * FROM ##GlobalTemp in the Source Query for the Data Transform Task, then move to the Destination tab. Click Define Columns (might not be necessary), Populate from Source, Execute, and you should be set!
Nice article, and just what I needed.
One thing, though. About:
–Check for temp table and delete if it exists
IF object_id(‘##GlobalTemp’) IS NOT NULL
BEGIN
DROP TABLE ##GlobalTemp
END
My global temp table’s object_id is returning NULL whether it exists or not.. Just FYI.
this definitely helped me get to a point in the DTS creation, but I’m still having some issues.
the layout of my package is: create global temp table (sql task) –> populate global temp table (sql task) –> transform data task with select query going to text file output –> transform data task with select query going to text file output (2) –> transform data task with select query going to text file output (3)
when run, the package makes creates the 1st file fine but fails on the other 2 saying invalid object [temp table name]. so it seems to be dropping the temp table after that first transform data task and can’t see it for the other 2.
any ideas on how to get this working?
Adam, I sent your question on the Jody, the one who wrote this particular post. Hopefully we’ll hear back from him.