SQL SERVER – How to Drop Temp Table – Check Existence of Temp Table

I have received following questions numerous times:

“How to check existence of Temp Table in SQL Server Database?”

“How to drop Temp Table from TempDB?”

“When I try to drop Temp Table I get following error. Msg 2714, Level 16, State 6, Line 4
There is already an object named ‘#temp’ in the database.
How can I fix it?”

“Can we have only one Temp Table or we can have multiple Temp Table?”

“I have SP using Temp Table, when it will run simultaneously, will it overwrite data of temp table?”

In fact I have already answer this question earlier in one of my blog post. I have even explained how Temp Table works in TempDB and how they are managed.

Visit my earlier written article for answers to questions listed above.

SQL SERVER – Fix : Error : Msg 2714, Level 16, State 6 – There is already an object named ‘#temp’ in the database

Reference : Pinal Dave (https://blog.sqlauthority.com)

, ,
Previous Post
SQLAuthority News – TechEd India 2009 – Day 3 – Product Group Meeting – Final Presentations – Meeting Friends
Next Post
SQL SERVER – Fix : Management Studio Error : Saving Changes in not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created

Related Posts

37 Comments. Leave new

  • Below is how you can check and delete temp tables.

    IF EXISTS
    (
    SELECT *
    FROM tempdb.dbo.sysobjects
    WHERE ID = OBJECT_ID(N’tempdb..#MyTempTable’)
    )
    BEGIN
    DROP TABLE #MyTempTable
    END

    Reply
  • i tired using it
    i get sysntax error
    Incorrect syntax near ‘)’

    Reply
  • “i tired using it
    i get sysntax error
    Incorrect syntax near ‘)’ ” – SVK

    I suspect that this is because the character ’ (single quote) is invalid, it should be ‘ (apostrophe) instead.

    What I suspect has happened is that either the database for this website is converting apostrophes to single quote or the developer of the query pasted their query into MS Word which then assumed that the character should be a single quote and converted it.

    Solution: Try replacing all single quote characters to apostrophes. I did that and it works fine for me.

    Reply
  • Thank You Very Much
    it work perfectly with me

    Reply
  • Thank You sir

    Reply
  • Thanks! This was a great place to get a working solution for us. I modified it slightly:
    Select
    1
    FROM
    tempdb.dbo.sysobjects (nolock)
    WHERE
    ID = OBJECT_ID(N’tempdb.dbo.#batch’) and
    [Type] = ‘U’

    Our deployment team doesn’t like “Select *” for any reason, also added a (nolock) hint and limited it User ‘U’ object types only.

    Reply
  • Well when I try this I get a NULL value returned.

    select my_id, my_name
    into #tmp_mwj
    from dbo.my_table

    (creates table)

    select OBJECT_ID(‘dbo.#tmp_mwj’)
    (returns null)

    SELECT 1
    FROM dbo.sysobjects
    WHERE ID = OBJECT_ID(‘dbo.#tmp_mwj’)

    Reply
  • Hi Mike,

    The objectid of temporary table can be viewed by this query:

    select OBJECT_ID(‘tempdb.dbo.#tmp_mwj’)

    Regards,
    Pinal Dave

    Reply
  • Here is the Example.
    I have two tables Manager and Employee each has different Empids and Salaries so i want to get the Maximum sal of the Two tables.for that i have used #temptables.

    select salary into #temptbks from Empsal Union select salary from Managers;select max(salary)as salary from #temptbks;
    drop table #temptbks.As soon as temptable is created U should delete so that u can use that same table if the Original table has been Updated.The #temptable will be available in that database itself.In sql-server.If u want this query in asp.net than go for Stored procedure.

    Reply
  • Thanks, this worked for me as well. I had been going nuts trying to figure this out.

    IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N’tempdb..#t’)) BEGIN DROP TABLE #t END

    Reply
  • Here i really like to make a comment that i m getting an issue with the tempdb as there are some blocking seen while there is a temp table created wthin the procedures there.

    Eg there is a procedure wthin the procedure the are nested procedure called.

    Reply
  • IF OBJECT_ID(‘tempdb..#TEMP’) IS NOT NULL
    BEGIN
    DROP TABLE #TEMP
    END

    Reply
  • Hi Pinal,
    Nice Article.

    Reply
  • Nice Article! as OBJECT_ID() function returns details related to temp table in context of current session so won’t get multiple entries for tables starting with same name, as system suffixes some numbers after local temp table. so it will be tough to identify row related to session in context.

    Reply
  • Hi is it good to write many stored procedure and call one with in one instead of writing one single 6 to 15 pages stored procedure.
    I am assuming that sp execute like parallel programming.

    Reply
  • Useful & to the point .Keep it up

    Reply
  • sivaramakrishnan
    August 27, 2011 5:33 pm

    how to delete ##teemp table please clarify this…

    Reply
    • Mrityunjay Singh
      September 14, 2011 5:57 pm

      sivaramakrishnan

      Sorry i am late, But if you code same type or error again then please use this code to solve your error

      SELECT @MYQUERY = ‘IF EXISTS ( SELECT * FROM TEMPDB..SYSOBJECTS WHERE NAME LIKE ”%##PF’ + CAST ( @@SPID AS VARCHAR ) + ‘%” ) DROP TABLE ##PF’ + CAST ( @@SPID AS VARCHAR )
      EXECUTE ( @MYQUERY )

      Reply
  • Mrityunjay Singh
    September 14, 2011 5:54 pm

    sivaramakrishnan,

    please use this to solve your error

    SELECT @MYQUERY = ‘IF EXISTS ( SELECT * FROM TEMPDB..SYSOBJECTS WHERE NAME LIKE ”%##PF’ + CAST ( @@SPID AS VARCHAR ) + ‘%” ) DROP TABLE ##PF’ + CAST ( @@SPID AS VARCHAR )
    EXECUTE ( @MYQUERY )

    Reply
  • shorter of all
    if OBJECT_ID(‘tempdb..#temp’)>0 DROP TABLE #temp

    Reply
    • if OBJECT_ID(‘tempdb..#temp’)>0 DROP TABLE #temp
      This will not work on SQL Server 2012, as OBJECT_ID for temp tables will return negative values

      Reply
  • hi,

    i am facing a problem that is, there are too many temp tables in tempdb. i have already told to my team not to forget dropping it after use. by the way i just want to write a script that “remove ALL temp tables without knowing its name programmatically” and i will schedule it as a nightly job to ensure that the tempdb will not be overgrown. how can i do such a things or is it possible to do?

    Reply

Leave a Reply

Menu