views:

92

answers:

5

I see some code where the author has truncated a temp table immediately before dropping the temp table. Is there a reason for doing this?

TRUNCATE TABLE #Temp
DROP TABLE #Temp
A: 

Possibly to see if the TRUNCATE command throws an exception due to existing foreign keys?

davek
I suppose that isn't out of the realm of possibilities but this particular code is not in any sort of error handling block
Joe Philllips
I can't think of any other reason then, other than a wrong assumption (as mentioned in the answer from CodeByMoonlight)
davek
+1  A: 

Possibly the author was under the impression that DROP TABLE would be quicker if the table was already empty and knew that TRUNCATE would be quicker than DELETE.

CodeByMoonlight
+1  A: 

Could possibly be a knucklehead. (not that I'm perfect).

codingguy3000
+1  A: 

On very large temp tables, it's sometimes faster to truncate first then drop because truncate simply moves a pointer. It's normally not needed since temp tables drop on their own.

Jeff Moden
+1  A: 

Another reason is that a DROP TABLE is a fully logged operation, so by truncating first (which is never logged) you lower transactional logging overhead.

Alex K.
Sounds reasonable
Joe Philllips
I've learned recently that a truncate is logged (but not like a delete is) but I understand your point
Joe Philllips