Error "There is already an object named '#TEMP' in the database"

1

I am performing a load by using a temporary table in Microsoft SQL Server 2008 .

SELECT CAMPO1,
        CAMPO2,
        CAMPO3,
        CAMPO4 
     INTO #TEMP 
     FROM DADOS

In the query, I use the following command, to check if the temporary table exists, if the table exists I do the DROP TABLE , as the example below.

IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL   
BEGIN       
    DROP TABLE #TEMP    
END

After the executed execution the table continues to be created in tempdb

If executed only:

DROP TABLE #TEMP

The message is resumed:

  

Msg 3701, Level 11, State 5, Line 1

     

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

You can only reload after disconnecting from the server.

I ask for help.

    
asked by anonymous 28.01.2016 / 14:51

1 answer

0

Try the code below

IF EXITS( SELECT TOP 1 * FROM tempdb..#TEMP)

BEGIN

DROP TABLE #TEMP

END
    
28.01.2016 / 18:06