There is already an object named '##Temp' in the database

phoenies picture phoenies · Aug 13, 2010 · Viewed 43.5k times · Source

I have a stored procedure on SQL Server 2000. It contains:
select ... into ##Temp ...
...
drop table ##Temp

When I run the stored procedure with ADO a second time, it prompts:
There is already an object named '##Temp' in the database.
Could anyone kindly tell me what's wrong?

Answer

Jon Freedman picture Jon Freedman · Aug 13, 2010

You should re-write your stored proc to drop the temp table if it exists, then you won't ever have this issue

IF (SELECT object_id('TempDB..##Temp')) IS NOT NULL
BEGIN
    DROP TABLE ##Temp
END