March 1, 2007 at 7:16 am
I have a few questions about temporary tables:
1. Is there a way to know which tables have not been dropped? Some statement like "SELECT TempTableActive"
2. When does a temp table drop. Obviously when I do a DROP TABLE. I'm assuming if I reboot the computer it also goes away. Any other times?
3. How do I create an identity field in a temp table? Example:
SELECT Seq (identity), AccoutId, Amount INTO #TempA FROM MyTable
4. Any other tricks or tips that might be useful on temp tables would be appreciated.
Thanks!!!
March 1, 2007 at 7:28 am
IN 2005 this appears to answer point 1
Create
Table #XXXX(id int)
Select
* From tempdb.INFORMATION_SCHEMA.TABLES
where
TABLE_name like '%x%'
drop
table #XXXX
Select
* From tempdb.INFORMATION_SCHEMA.TABLES
where
TABLE_name like '%x%'
March 1, 2007 at 8:36 am
"When does a temp table drop. Obviously when I do a DROP TABLE. I'm assuming if I reboot the computer it also goes away. Any other times?"
A temp table (at least one that starts with one #) is only visible to the connection that created it. So as soon as your connection ends, the temp table disappears.
"How do I create an identity field in a temp table?"
Use the CREATE TABLE statement the same as you would for a normal table.
John
March 2, 2007 at 8:35 am
Indices are allowed on #temp tables, and depending on the number of rows in the table, can dramatically affect performance.
Don't have a #temp table in one proc and call a proc with a #temp table of the same name. Worked in 6.5, doesn't work in 2000.
March 2, 2007 at 8:39 am
Well that still works.. depending on what you want to do with it. The child proc will be able to access the temp table as if it had created it.
March 3, 2007 at 2:06 pm
Sw...
I'd like to recommend that you look up "Temporary Tables" in Books Online as well as making a visit to the following web-site...
http://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k
Between the two, you should have just about all the information you could ever ask for on temporary tables.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 6, 2007 at 7:31 am
Not sure about
in this link metion about
Transactions that involve table variables last only for the duration of an update on the table variable. Therefore, table variables require less locking and logging resources. Because table variables have limited scope and are not part of the persistent database, transaction rollbacks do not affect them.
Thx.
March 6, 2007 at 4:53 pm
It's correct... there is a very minor amount of logging for Temp Tables and none for table variables. The fact that Temp Tables can use statistics and Table Variables cannot usually makes up for the small difference in performance that that bit of logging does.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 7, 2007 at 5:48 am
The text has been made a bit confusing by the red/blue coloring... so just to make sure nobody misunderstands that : not table variables, but transactions (involving these table variables) last only for the duration of update.
Transactions that involve table variables last only for the duration of an update on the table variable.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply