Pro Programmer: Local and Global Temporary Tables and Table Variables

Wednesday, August 15, 2012

Local and Global Temporary Tables and Table Variables


Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server.

Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.


Some More Definitions from StackOverFlow:

  • Table variables (DECLARE @t TABLE) are visible only to the the
    connection that creates it, are stored in RAM, 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 the connection that created it is closed.
  • Tempdb permanent tables (USE tempdb CREATE TABLE t) are visible to everyone, and are deleted when the server is restarted.



  • No comments:

    Post a Comment