"Level" 本地临时表中的范围 sql 服务器
"Level" scope in local temporary tables sql server
我正在读一本书提到
Local temporary tables are visible throughout the level that created them, across batches, and in all inner levels of the call stacks. So if you create a temporary table in a specific level in your code and then execute a dynamic batch or a stored procedure, the inner batch can access the temporary table.
和
Table variables are not visible even across batches in the same level
从其他来源,我了解到本地临时表对创建它的会话可见,并在会话关闭时销毁。
那么这里的"level"和"inner levels of the call stacks"是什么意思?
想象一个名为 A
的存储过程,然后依次调用两个存储过程 B
和 C
1.
这些存储过程中的每一个都会创建一个具有相同名称的临时 table,(当然)以 #
为前缀(因此,A
创建 #A
)作为他们的第一个动作,在 运行 任何进一步的代码之前。
您在查询分析器中执行以下代码:
CREATE TABLE #F (ID INT NOT NULL)
EXEC A
GO
EXEC A
A
中的代码可以与 tables #F
和 #A
.
一起使用
B
中的代码可以与 tables #F
、#A
和 #B
一起使用
C
中的代码可以与 tables #F
、#A
和 #C
一起使用
尽管 B
和 C
在同一个 "level"(它们都是从 A
调用的),C
无法访问临时文件 [= B
创建的 58=](当 B
退出时它是 destroyed)。
1
CREATE PROCEDURE A
AS
CREATE TABLE #A (ID INT NOT NULL)
EXEC B
EXEC C
我正在读一本书提到
Local temporary tables are visible throughout the level that created them, across batches, and in all inner levels of the call stacks. So if you create a temporary table in a specific level in your code and then execute a dynamic batch or a stored procedure, the inner batch can access the temporary table.
和
Table variables are not visible even across batches in the same level
从其他来源,我了解到本地临时表对创建它的会话可见,并在会话关闭时销毁。
那么这里的"level"和"inner levels of the call stacks"是什么意思?
想象一个名为 A
的存储过程,然后依次调用两个存储过程 B
和 C
1.
这些存储过程中的每一个都会创建一个具有相同名称的临时 table,(当然)以 #
为前缀(因此,A
创建 #A
)作为他们的第一个动作,在 运行 任何进一步的代码之前。
您在查询分析器中执行以下代码:
CREATE TABLE #F (ID INT NOT NULL)
EXEC A
GO
EXEC A
A
中的代码可以与 tables #F
和 #A
.
B
中的代码可以与 tables #F
、#A
和 #B
C
中的代码可以与 tables #F
、#A
和 #C
尽管 B
和 C
在同一个 "level"(它们都是从 A
调用的),C
无法访问临时文件 [= B
创建的 58=](当 B
退出时它是 destroyed)。
1
CREATE PROCEDURE A
AS
CREATE TABLE #A (ID INT NOT NULL)
EXEC B
EXEC C