Local and global temporary tables in SQL Server

andrew Sullivan picture andrew Sullivan · May 27, 2010 · Viewed 282.1k times · Source

What is the difference between local and global temporary tables in SQL Server?

Answer

Anthony Faull picture Anthony Faull · May 27, 2010
  • Table variables (DECLARE @t TABLE) are visible only to the connection that creates it, and are deleted when the batch or stored procedure ends.

  • Local temporary tables (CREATE TABLE #t) are visible only to the connection that creates it, and are deleted when the connection is closed.

  • Global temporary tables (CREATE TABLE ##t) are visible to everyone, and are deleted when all connections that have referenced them have closed.

  • Tempdb permanent tables (USE tempdb CREATE TABLE t) are visible to everyone, and are deleted when the server is restarted.