17

Where do temporary tables get stored in a database? I want to drop a temporary table if it already exists. I can do this for securable tables by querying at information schema but I don't know where temporary tables are stored.

djvg
  • 11,722
  • 5
  • 72
  • 103
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

5 Answers5

22

Temporary tables are stored in tempdb Database. There are various ways to check if a temp table exists outlined here: Check If Temporary Table Exists.

Giorgi
  • 30,270
  • 13
  • 89
  • 125
8

Temporary tables gets stored in tempdb database which is present in SystemDatabase or SystemDatabase -> tempdb -> Temporary Tables

4

TempDb Will In in SystemDatabase.Temp tables are stored here.

CJBS
  • 15,147
  • 6
  • 86
  • 135
sanjay
  • 41
  • 1
3

Store at this table

SELECT *
FROM   tempdb.sys.tables

Delete query:

DECLARE @sql NVARCHAR(MAX)
SELECT @sql = ISNULL(@sql + ';', '') + 'drop table ' + QUOTENAME(NAME)
FROM   tempdb..sysobjects
WHERE  NAME LIKE '#%'

EXEC (@sql)
TT.
  • 15,774
  • 6
  • 47
  • 88
Gurung
  • 159
  • 1
  • 4
0

Here you can find all your temp tables (both local and global) which are stored and active using SSMS.

enter image description here

Nandha MV
  • 469
  • 4
  • 14