What is the difference between local and global temporary tables in SQL Server?
相关问题
- sql execution latency when assign to a variable
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
相关文章
- Entity Framework 4.3.1 failing to create (/open) a
- Code for inserting data into SQL Server database u
- Delete Every Alternate Row in SQL
- Linux based PHP install connecting to MsSQL Server
- SQL Azure Reset autoincrement
- How do we alias a Sql Server instance name used in
- Is recursion good in SQL Server?
- How can I convert a OLE Automation Date value to a
Quoting from Books Online:
Local temporary tables are visible only in the current session; global temporary tables are visible to all sessions.
Temporary tables are automatically dropped when they go out of scope, unless explicitly dropped using DROP TABLE:
1.) A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.
2.) A global temporary table remains in the database permanently, but the rows exist only within a given connection. When connection is closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.
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.It is worth mentioning that there is also: database scoped global temporary tables(currently supported only by Azure SQL Database).
I find this explanation quite clear (it's pure copy from Technet):