June 7, 2006 at 11:10 am
Not seen this difference documented..
Code below illustrates one other difference between temp tables and table variables. That that dynamic sql can see temp tables created by calling script but cant see table variables. Presumably this reveals some substantive difference between the way they are treated behind the scenes.
-----Temp Table
create table #test (id int identity, test nvarchar(20))
declare @sql nvarchar(4000)
select @sql ='insert #test (test) select ''ohdear'''
print @sql
exec sp_executesql @sql
select * from #test
------Table variable
declare @tab table (id int identity, test nvarchar(20))
declare @sql2 nvarchar(4000)
select @sql2 ='insert @tab (test) select ''ohdear'''
print @sql2
exec sp_executesql @sql2
select * from @tab
Anyone know why this is and if i could use a table variable here?
June 7, 2006 at 11:22 am
A variable is constrained by scope. It doesn't matter if it's a simple int or varchar, or a table variable, regardless, it only has scope to the batch containing it.
------Integer variable
declare @test-2 int
Select @test-2 = 123
print @test-2
declare @sql2 nvarchar(4000)
select @sql2 ='print @test-2'
print @sql2
exec sp_executesql @sql2
A temp table is a DB object in tempdb, much different than a locally scoped variable.
June 7, 2006 at 11:27 am
See these articles for more information....
http://www.sqlservercentral.com/columnists/RDyess/temptabl.asp
http://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k
-Luke.
June 7, 2006 at 12:35 pm
Yep Thanks. of course the hint is in the name 'Tabke VARIABLE'.
WQas reading the thread in responce to the recent article on temp tables where the general concensus is that they arent that different.
Scope is certainly one crucial difference which you would have thought had implications for performance.
June 7, 2006 at 1:08 pm
>>Scope is certainly one crucial difference which you would have thought had implications for performance.
Why would scope impact performance ? Consider #Temp tables versus ##Temp tables. Again, difference is only scope, but no correlation to performance.
June 7, 2006 at 1:31 pm
'A temp table is a DB object in tempdb, much different than a locally scoped variable'
If enough data goes into a table variable it will create an object in tempdb. So it is not clear that local scope and creation of objects in tempdb are mutally exclusive as your post appears to claim.
But of course as demostrated by ## and # table there isnt a theoretical reason why scope shoud effect performance but perhaps the way SQL implents objects of different scope is distinct enough to create performance differences.
There seems to be no concensus on the facts of how TVs and TTs are implemented and a better understanding of the relation of scope to tempdb may shed some light on this.
June 7, 2006 at 8:46 pm
Both Temp Tables and Table Variables start out in memory and both spill into TempDB when they get big enough... there are many other differences, as well...
http://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k
...draw your own conclusions on what turns out to be a fairly controversial subject...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 8, 2006 at 5:33 pm
The more tightly defined scope allows SQL to possibly make better judgements about the state of the table - hence less recompiles in stored procs. Table variables, being tightly scoped, are not available universally to the connection. Temp tables have then connection as their scope - thus you can have a number of chained stored procs use them, or even multiple calls from your app (within the one connection of course).
Pros and cons either way - sometimes you may need to create additional indices on your temp table... This cannot be done with a table variable. I tend to use table variables and resort to temp tables when I need to allow an extra index due to some filtering, ordering or grouping, or when it needs to be accessed by another stored proc (this is usually rare but does happen). Finally, if you have a recursive proc that needs to accumulate results then a #temp table can be handy but you can usually unwind the recursion and turn it into a loop and stack anyway.
But Jeff's correct - both start out in memory and spill into TempDB.
June 9, 2006 at 4:13 am
I didn't go through the links but another advantage of the table variable is there is no logging so you don't have that overhead.
June 9, 2006 at 7:37 am
Does anyone have a Microsoft link or know the lookup in BOL that says that Temp Tables are logged (or not?). I know there is a log file associated with TempDB but I've never seen it grow to hellashish proportions even with very large Temp Tables?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 14, 2006 at 2:40 am
Jeff
This (and other similar threads) seems to suggest that there is logging in tempdb. Don't know whether there's anything from Microsoft confirming this.
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=5&messageid=13898&p=2
John
June 14, 2006 at 7:51 am
Thanks for looking, John. If I find anything from Microsoft, I'll be sure to post it.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply