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)
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
i tired using it
i get sysntax error
Incorrect syntax near ‘)’
“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.
Thank You Very Much
it work perfectly with me
Thank You sir
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.
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’)
Hi Mike,
The objectid of temporary table can be viewed by this query:
select OBJECT_ID(‘tempdb.dbo.#tmp_mwj’)
Regards,
Pinal Dave
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.
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
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.
IF OBJECT_ID(‘tempdb..#TEMP’) IS NOT NULL
BEGIN
DROP TABLE #TEMP
END
Hi Pinal,
Nice Article.
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.
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.
Useful & to the point .Keep it up
how to delete ##teemp table please clarify this…
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 )
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 )
shorter of all
if OBJECT_ID(‘tempdb..#temp’)>0 DROP TABLE #temp
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
That code works perfectly in SQL Server 2012.
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?