问题描述
问题 1:我在 SQL Server 2008 中使用全局临时表.但是一旦我的连接关闭,这个临时表就会被丢弃.有什么办法可以禁用自动丢弃
Question 1: I am using a global temp tables in SQL Server 2008. But once my connection is closed this temp is dropped. Is there any way to disable auto drop
问题 2:如果两个连接正在访问同一个全局临时表,而另一个连接正在访问尝试删除该全局临时表,SQL Server 是否正确处理此同步?
Question 2: If two connections are accessing same global temp table and another connection istrying to delete that global temp table, does SQL Server handles this synchronization properly?
推荐答案
您可以在存储过程中创建全局临时表并使用启动选项对其进行标记.
You can create your global temp tables in a stored procedure and mark it with the startup option.
SQL Server 为在启动过程中创建的所有全局临时表维护一个大于零的引用计数.
SQL Server maintains a reference count greater than zero for all global temporary tables created within startup procedures.
一些示例代码
CREATE PROC dbo.CreateGlobalTempTables
AS
CREATE TABLE ##my_temp_table
(
fld1 INT NOT NULL PRIMARY KEY,
fld2 INT NULL
);
GO
EXEC dbo.sp_procoption 'dbo.CreateGlobalTempTables', 'startup', 'true';
全局临时表将在启动时自动创建并持续存在,直到有人明确删除它.
The global temporary table will be created automatically at startup and persist until someone explicitly drops it.
这篇关于在 SQL Server 2008 中,是否可以禁用全局临时表的自动删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!