November 2, 2012 at 11:51 pm
Hi,
I have a SP in which i create 3 temp tables. and get result from those tables. But at the end of sp do i need to drop those temp tables ? or those will be deleted from memory automatically ?
in versions of 2000,2005,2008
thanks
November 2, 2012 at 11:56 pm
its always good idea to write drop statement in your sp.
However temp tables gets automatically dropped when you close your Query window .
-----------------------------------------------------------------------------
संकेत कोकणे
November 3, 2012 at 12:12 am
thanks yaar
November 3, 2012 at 2:09 am
Temp tables are automatically dropped as soon as they go out of scope (the proc that they were created in completes) or the connection that created them closes.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 3, 2012 at 10:07 am
KcV (11/2/2012)
Hi,I have a SP in which i create 3 temp tables. and get result from those tables. But at the end of sp do i need to drop those temp tables ? or those will be deleted from memory automatically ?
in versions of 2000,2005,2008
thanks
No... you don't need to drop temp tables.
That notwithstanding, I tend to do a conditional drop at the beginning of a sproc and it has nothing to do with any effect on the spoc. Rather, they are an artifact from development and testing prior to conversion to a stored procedure. I leave the conditional drop in place for the next person who may have to troubleshoot the code. It costs almost nothing to do the conditional drop and it also anyone reading the code know that there are temp tables somewhere in the proc.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2012 at 10:12 am
Jeff Moden (11/3/2012)
KcV (11/2/2012)
Hi,I have a SP in which i create 3 temp tables. and get result from those tables. But at the end of sp do i need to drop those temp tables ? or those will be deleted from memory automatically ?
in versions of 2000,2005,2008
thanks
No... you don't need to drop temp tables.
That notwithstanding, I tend to do a conditional drop at the beginning of a sproc and it has nothing to do with any effect on the spoc. Rather, they are an artifact from development and testing prior to conversion to a stored procedure. I leave the conditional drop in place for the next person who may have to troubleshoot the code. It costs almost nothing to do the conditional drop and it also anyone reading the code know that there are temp tables somewhere in the proc.
I do the same thing.
IF OBJECT_ID(N'tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable;
CREATE TABLE #MyTempTable (column list);
No need to drop them at the end, but I find the above useful for refactoring/debugging/documenting/etc.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply