SQL Server 中的本地和全局临时表
SQL Server 中的本地临时表和全局临时表有什么区别?
What is the difference between local and global temporary tables in SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
SQL Server 中的本地临时表和全局临时表有什么区别?
What is the difference between local and global temporary tables in SQL Server?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
表变量 (
DECLARE @t TABLE
) 仅对创建它的连接可见,并在批处理或存储过程结束时被删除。本地临时表 (
CREATE TABLE #t
) 仅对创建它的连接,并在连接被删除时删除
已关闭。
全局临时表 (
CREATE TABLE ##t
) 对所有人都可见,并在引用它们的所有连接都关闭时被删除。
Tempdb 永久表(
USE tempdb CREATE TABLE t
)对以下对象可见每个人,并且在服务器重新启动时被删除。
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 theconnection 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 toeveryone, and are deleted when the server is restarted.
我发现这个解释非常清楚(它是来自 Technet 的纯副本):
I find this explanation quite clear (it's pure copy from Technet):
1.) 本地临时表仅在连接期间存在,或者如果在复合语句内定义,则在复合语句期间存在。
2.) 全局临时表永久保留在数据库中,但行仅存在于给定连接中。当连接关闭时,全局临时表中的数据消失。但是,表定义仍保留在数据库中,以便下次打开数据库时进行访问。
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.
引用在线书籍:
本地临时表仅在当前会话中可见;全局临时表对所有会话都可见。
临时表超出范围时会自动删除,除非使用 DROP TABLE 显式删除:
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:
我没有看到任何答案向用户显示我们可以在哪里找到全局临时表。在 SSMS 中导航时,您可以在同一位置查看本地和全局临时表。下面的屏幕截图取自此链接。
数据库 -->系统数据库 -->临时数据库 -->临时表
I didn't see any answers that show users where we can find a Global Temp table. You can view Local and Global temp tables in the same location when navigating within SSMS. Screenshot below taken from this link.
Databases --> System Databases --> tempdb --> Temporary Tables
本地临时表:如果您创建本地临时表,然后打开另一个连接并尝试查询,您将收到以下错误。
临时表只能在创建它们的会话中访问。
全局临时表:
有时,您可能想要创建一个可供其他连接访问的临时表。在这种情况下,您可以使用全局临时表。
仅当所有引用全局临时表的会话都关闭时,全局临时表才会被销毁。
Local temporary tables: if you create local temporary tables and then open another connection and try the query , you will get the following error.
the temporary tables are only accessible within the session that created them.
Global temporary tables:
Sometimes, you may want to create a temporary table that is accessible other connections. In this case, you can use global temporary tables.
Global temporary tables are only destroyed when all the sessions referring to it are closed.
值得一提的是,还有: 数据库范围的全局临时表(目前仅受 Azure SQL 数据库支持)。
It is worth mentioning that there is also: database scoped global temporary tables(currently supported only by Azure SQL Database).