Drop temp tables

  • My stored proc produces a number of temp tables which may get quite large. Which are created may depend on the data. I need to drop them if they exist when they are no longer necessary. Having trouble with the

    IF EXISTS (SELECT * from what?

    Can someone post a reliable means of determining if the temp table exists in tempdb ?

    Thanks for your help

  • I would drop them at end of stored procedure using command "drop table #yourtemptablename".

    Edited by - allen_cui on 10/29/2003 09:52:33 AM

  • I made a real mole hill out of a mountain on this one. The tables are created by a select ... into #table so even if no rows are selected an empty table is created. I thought the empty table would not be created. Goes to show - need to do the experiment.

    Thank you

  • Your if exists could look like the following...

    
    
    IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id('tempdb..#temptable'))
    DROP TABLE #temptable


    or a simpler version...
    IF object_id('tempdb..#temptable') IS NOT NULL
    DROP TABLE #temptable

    Gary Johnson

    Microsoft Natural Language Group

    DBA, Sr. DB Engineer




    Gary Johnson
    Microsoft Natural Language Group
    DBA, Sr. DB Engineer

    This posting is provided "AS IS" with no warranties, and confers no rights. The opinions expressed in this post are my own and may not reflect that of my employer.

  • I didn't think you had to explicitly drop temp tables created with the # prefix?

    I thought SQL Server cleaned these up for you.

  • While SQL Server will drop them for you, I always clean up after myself when I can. It's just a good habit to do so. Also prevents errors if you run an sp multiple times without closing the window in QA.

    Gary Johnson

    Microsoft Natural Language Group

    DBA, Sr. DB Engineer




    Gary Johnson
    Microsoft Natural Language Group
    DBA, Sr. DB Engineer

    This posting is provided "AS IS" with no warranties, and confers no rights. The opinions expressed in this post are my own and may not reflect that of my employer.

  • Also, one thing to point out is that you can cause blocking issues in tempdb by doing SELECT INTO ... #Tmp

    Better to explicitly create and drop the tables.

    Hope this helps

    Phill Carter

    --------------------

    Colt 45 - the original point and click interface

    --------------------
    Colt 45 - the original point and click interface

  • Everything I've read has made me decide to always explicitly drop the temp tables at the end of my stored procedure in addition to checking if they exist at the beginning of my stored procedure and dropping them if they exist.

  • I always clean up after myself as well.

    Gary, Even if you don't drop the temp table within your stored proc you can run it multiple times within QA. When the proc completes any temp tables created in the proc will no longer exist.

  • if object_ID('Tempdb..#Test') is not null drop table #Test

    Signature is NULL

  • Oops...gljjr already said that. Never mind.

    Signature is NULL

  • I agree about the cleanup - coding explicit behavior is a good practice to follow.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply