• BTW, the best way to check for the existance of a temporary table in your current session is:

    IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULL

    Note that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use:

    IF OBJECT_ID('tempdb..#YourTempTable') > 0

    However, the next version of SQL (code named Denali) is returning an object_id with a negative value, so the >0 won't work there, and it will need to be recoded.

    Every other method I've seen for checking for the existence of a temp table fails when the temp table is created in a different sessions - those methods either crash, or return a positive (yes, it exists) incorrectly.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2