• umar iqbal (3/19/2009)


    if you want to clean up tempdb u can schedule this script. it will remove temp tables that are older than 12 hrs.

    Did you test out that script?

    TempDB is set up in such a way that a particular connection can only drop temp tables that were created on that connection. if you try to drop a temp table that another connection created, you'll get an error similar to this:

    select name from sys.tables

    returns - #TEMP_______________________________________________________________________________________________________________000000000003

    drop table tempdb..#TEMP_______________________________________________________________________________________________________________000000000003 returns

    Cannot drop the table '#TEMP_______________________________________________________________________________________________________________000000000003', because it does not exist or you do not have permission.

    The only time that script will work is if people are creating 'permanent' tables in tempDB.

    Mike: Check that you don't have lots of open transactions. It also might be SQL caching the table's definitions, if they're frequently used. It's an optimisation in 2005 and higher. If temp tables or table variables (and what you have is a table variable) are frequently used then, instead of dropping them, SQL just 'truncates' them, leaving the definition. It saves the effort of recreating the table next time it's needed.

    Are the tables being left with lots of rows in them? How are you checking that (since you can't query any temp table that you didn't create)? You mention 65 GB in the log, that'll just be from tempDB usage, not left over temp tables.

    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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass