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)对每个人都可见,并在重新启动服务器时被删除.
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:
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连接可见.
| 归档时间: |
|
| 查看次数: |
258069 次 |
| 最近记录: |