SQL Server 中本地临时表和全局临时表有什么区别?
SQL Server 中本地临时表和全局临时表有什么区别?
表变量 (DECLARE @t TABLE
) 仅对创建它的连接可见,并在批处理或存储过程结束时被删除。
本地临时表 (CREATE TABLE #t
) 仅对创建它的连接可见,并在连接关闭时被删除。
全局临时表 (CREATE TABLE ##t
) 对所有人可见,并在所有引用它的连接都关闭时被删除。
Tempdb永久表 (USE tempdb CREATE TABLE t
) 对所有人可见,并在服务器重新启动时被删除。
临时表有两种类型:本地和全局。本地临时表仅在与创建或引用这些表时连接到 SQL Server 实例的同一连接期间,对其创建者可见。在用户断开与 SQL Server 实例的连接后,本地临时表将被删除。全局临时表对任何用户和连接都可见,并在所有引用该表的用户从 SQL Server 实例断开连接时被删除。
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
本地临时表的范围仅限于当前用户会话,即当前查询窗口。如果关闭当前查询窗口或打开新的查询窗口并尝试查找上述创建的临时表,则会出现错误。
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
全局临时表对所有 SQL Server 连接可见,而本地临时表仅对当前 SQL Server 连接可见。
全局临时表对所有 SQL Server 连接可见,而本地临时表仅对当前 SQL Server 连接可见。
引用自 Books Online:
本地临时表仅在当前会话中可见;全局临时表对所有会话均可见。
临时表在超出范围时会自动删除,除非使用 DROP TABLE 明确删除:
本地临时表:如果您创建了本地临时表,然后再打开另一个连接并尝试查询,则会收到以下错误。
临时表只能在创建它们的会话中访问。
全局临时表:有时,您可能希望创建一个可以被其他连接访问的临时表。在这种情况下,您可以使用全局临时表。
只有当所有引用它的会话关闭时,全局临时表才会被销毁。
Global temporary tables for SQL Server (initiated with ## table name) are stored in tempdb and shared among all users’ sessions across the whole SQL Server instance.
Azure SQL Database supports global temporary tables that are also stored in tempdb and scoped to the database level. This means that global temporary tables are shared for all users’ sessions within the same Azure SQL Database. User sessions from other databases cannot access global temporary tables.
-- Session A creates a global temp table ##test in Azure SQL Database testdb1 -- and adds 1 row CREATE TABLE ##test ( a int, b int); INSERT INTO ##test values (1,1); -- Session B connects to Azure SQL Database testdb1 -- and can access table ##test created by session A SELECT * FROM ##test ---Results 1,1 -- Session C connects to another database in Azure SQL Database testdb2 -- and wants to access ##test created in testdb1. -- This select fails due to the database scope for the global temp tables SELECT * FROM ##test ---Results Msg 208, Level 16, State 0, Line 1 Invalid object name '##test'
ALTER DATABASE SCOPED CONFIGURATION
GLOBAL_TEMPORARY_TABLE_AUTODROP = { ON | OFF }
APPLIES TO: Azure SQL Database (feature is in public preview)
Allows setting the auto-drop functionality for global temporary tables. The default is ON, which means that the global temporary tables are automatically dropped when not in use by any session. When set to OFF, global temporary tables need to be explicitly dropped using a DROP TABLE statement or will be automatically dropped on server restart.
With Azure SQL Database single databases and elastic pools, this option can be set in the individual user databases of the SQL Database server. In SQL Server and Azure SQL Database managed instance, this option is set in TempDB and the setting of the individual user databases has no effect.