I see more and more people asking how to check if a temporary table exists. How do you check if a temp table exists?

You can use IF OBJECT_ID(‘tempdb..#temp’) IS NOT NULL Let’s see how it works

T-SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--Create table 
USE Norhtwind 
GO 
 
CREATE TABLE #temp(id INT) 
 
--Check if it exists 
IF OBJECT_ID('tempdb..#temp') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 
 
--Another way to check with an undocumented optional second parameter 
IF OBJECT_ID('tempdb..#temp','u') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 
 
 
 
--Don't do this because this checks the local DB and will return does not exist 
IF OBJECT_ID('tempdb..#temp','local') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 
 
 
--unless you do something like this 
USE tempdb 
GO 
 
--Now it exists again 
IF OBJECT_ID('tempdb..#temp','local') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 
 
--let's go back to Norhtwind again 
USE Norhtwind 
GO 
 
 
--Check if it exists 
IF OBJECT_ID('tempdb..#temp') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 
--Create table 
USE Norhtwind 
GO 

CREATE TABLE #temp(id INT) 

--Check if it exists 
IF OBJECT_ID('tempdb..#temp') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 

--Another way to check with an undocumented optional second parameter 
IF OBJECT_ID('tempdb..#temp','u') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 



--Don't do this because this checks the local DB and will return does not exist 
IF OBJECT_ID('tempdb..#temp','local') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 


--unless you do something like this 
USE tempdb 
GO 

--Now it exists again 
IF OBJECT_ID('tempdb..#temp','local') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 

--let's go back to Norhtwind again 
USE Norhtwind 
GO 


--Check if it exists 
IF OBJECT_ID('tempdb..#temp') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 

now open a new window from Query Analyzer (CTRL + N) and run this code again

T-SQL
1
2
3
4
5
6
7
8
9
--Check if it exists 
IF OBJECT_ID('tempdb..#temp') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 
--Check if it exists 
IF OBJECT_ID('tempdb..#temp') IS NOT NULL 
BEGIN 
PRINT '#temp exists!' 
END 
ELSE 
BEGIN 
PRINT '#temp does not exist!' 
END 

It doesn’t exist and that is correct since it’s a local temp table not a global temp table

Well let’s test that statement

T-SQL
1
2
3
4
5
6
7
8
9
10
11
12
--create a global temp table 
CREATE TABLE ##temp(id INT) --Notice the 2 pound signs, that's how you create a global variable 
 
--Check if it exists 
IF OBJECT_ID('tempdb..##temp') IS NOT NULL 
BEGIN 
PRINT '##temp exists!' 
END 
ELSE 
BEGIN 
PRINT '##temp does not exist!' 
END 
--create a global temp table 
CREATE TABLE ##temp(id INT) --Notice the 2 pound signs, that's how you create a global variable 

--Check if it exists 
IF OBJECT_ID('tempdb..##temp') IS NOT NULL 
BEGIN 
PRINT '##temp exists!' 
END 
ELSE 
BEGIN 
PRINT '##temp does not exist!' 
END 

It exists, right?
Now run the same code in a new Query Analyzer window (CTRL + N)

T-SQL
1
2
3
4
5
6
7
8
9
--Check if it exists 
IF OBJECT_ID('tempdb..##temp') IS NOT NULL 
BEGIN 
PRINT '##temp exists!' 
END 
ELSE 
BEGIN 
PRINT '##temp does not exist!' 
END 
--Check if it exists 
IF OBJECT_ID('tempdb..##temp') IS NOT NULL 
BEGIN 
PRINT '##temp exists!' 
END 
ELSE 
BEGIN 
PRINT '##temp does not exist!' 
END 

And yes this time it does exist since it’s a global table

I have also added this to our wiki, read it here: Check If Temporary Table Exists