SQL Server中的本地和全局临时表

and*_*van 140 sql-server temp-tables global-temp-tables

SQL Server中的本地和全局临时表有什么区别?

Ant*_*ull 318

  • 表变量(DECLARE @t TABLE)仅对创建它的连接可见,并在批处理或存储过程结束时删除.

  • 本地临时表(CREATE TABLE #t)仅对创建它的连接可见,并在关闭连接时被删除.

  • 全局临时表(CREATE TABLE ##t)对所有人都可见,并且在引用它们的所有连接都已关闭时将被删除.

  • Tempdb永久表(USE tempdb CREATE TABLE t)对每个人都可见,并在重新启动服务器时被删除.

  • 另外值得指出的是:当创建它们的作用域关闭时,将删除本地临时表.因此,如果您在sproc中创建一个本地临时表,然后尝试在该sproc之外访问它 - 它将不存在. (52认同)
  • 包含tempdb永久表的+1. (11认同)
  • "当引用它们的所有连接都已关闭时,将被删除" - 引用它们的"是什么意思"?如果来自一个#1连接的StoredProc创建一个## TempTable,我可以在10分钟之后从另一个连接#2看到它(如果该连接#2在创建表时是活动的吗?)回答:全局临时表会自动丢弃创建表的会话结束,所有其他任务都停止引用它们.(在不同的答案中查看此页面的更多内容) (8认同)

Don*_*Don 105

I find this explanation quite clear (it's pure copy from Technet):

There are two types of temporary tables: local and global. 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.


Chr*_*ter 11

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:

  • A local temporary table created in a stored procedure is dropped automatically when the stored procedure completes. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process which called the stored procedure that created the table.
  • All other local temporary tables are dropped automatically at the end of the current session.
  • 当创建表的会话结束并且所有其他任务已停止引用它们时,将自动删除全局临时表.任务和表之间的关联仅在单个Transact-SQL语句的生命周期内维护.这意味着在创建会话结束时,在最后一个主动引用表的Transact-SQL语句完成时,将删除全局临时表.


Viv*_* S. 11

1.) 本地临时表仅在连接期间存在,或者,如果在复合语句中定义,则在复合语句的持续时间内存在.

本地临时表仅适用于创建表的SQL Server会话或连接(表示单个用户).创建表的会话已关闭时,将自动删除这些内容.本地临时表名称以单个哈希("#")符号开头.

CREATE TABLE #LocalTemp
(
 UserID int,
 Name varchar(50), 
 Address varchar(150)
)
GO
insert into #LocalTemp values ( 1, 'Name','Address');
GO
Select * from #LocalTemp
Run Code Online (Sandbox Code Playgroud)

本地临时表的范围存在于当前用户的当前会话中,意味着当前查询窗口.如果您将关闭当前查询窗口或打开一个新的查询窗口并尝试查找上面创建的临时表,它将给您错误.


2.) 全局临时表永久保留在数据库中,但行仅存在于给定连接中.关闭连接时,全局临时表中的数据将消失.但是,下次打开数据库时,表定义仍保留在数据库中以供访问.

全局临时表可用于所有SQL Server会话或连接(表示所有用户).这些可以由任何SQL Server连接用户创建,并且在关闭所有SQL Server连接时会自动删除这些用户.全局临时表名称以双哈希("##")符号开头.

CREATE TABLE ##GlobalTemp
(
 UserID int,
 Name varchar(50), 
 Address varchar(150)
)
GO
insert into ##GlobalTemp values ( 1, 'Name','Address');
GO
Select * from ##GlobalTemp
Run Code Online (Sandbox Code Playgroud)

全局临时表对所有SQL Server连接都可见,而本地临时表仅对当前SQL Server连接可见.

  • 您对全局临时表的定义是我希望它的行为方式(来自其他数据库),但我的测试表明,SQL Server中实际发生的情况是:_“创建临时表的会话自动删除全局临时表表结束,所有其他任务已停止引用它们” (2认同)