简体   繁体   English

SQL Server 2005和临时表范围

[英]SQL Server 2005 and temporary table scope

I've read around the subject of temporary tables and scope and all the answers i've seen don't seem to talk about one of my concerns. 我已经阅读了临时表和范围的主题,我所看到的所有答案似乎都没有谈到我的一个问题。

I understand that a local temporary table's scope is only valid withing the lifetime of a stored procedure or child stored procedures. 我知道本地临时表的范围仅对存储过程或子存储过程的生命周期有效。 However what is the situation with regard to concurency. 然而,关于确定性的情况如何。 ie if i have a stored procedure that creates a temporary table which is called from two different processes but from the same user/connection string, will that temporary table be shared between the two calls to that one stored procedure or will it be a case of each call to the stored procedure creates an unique temporary table instance. 即如果我有一个存储过程创建一个临时表,该临时表是从两个不同的进程调用但来自相同的用户/连接字符串,那么临时表是否会在对该一个存储过程的两次调用之间共享,或者是每次调用存储过程都会创建一个唯一的临时表实例。

I would assume that the temporary table belongs to the scope of the call to the stored procdure but i want to be sure before i go down a path with this. 我假设临时表属于对存储过程的调用范围,但我想确定在我走这条路径之前。

Local temporary tables (start with #) are limited to your session; 本地临时表(以#开头)仅限于您的会话; other sessions, even from the same user/connection string, can't see them. 其他会话,即使是来自同一用户/连接字符串,也看不到它们。 The rules for the lifetime depend on whether the local temporary table was created in a stored procedure: 生命周期的规则取决于是否在存储过程中创建了本地临时表:

  • A local temporary table that is created in a stored procedure is dropped when the procedure ends; 当过程结束时,将删除在存储过程中创建的本地临时表; other stored procedures, or the calling process, can't see them. 其他存储过程或调用进程无法看到它们。
  • Other local temporary tables are dropped when the session ends. 会话结束时,将删除其他本地临时表。

Global temporary tables (start with ##) are shared between sessions. 全局临时表(以##开头)在会话之间共享。 They are dropped when: 他们在下列时被删除:

  • The session that created them ends 创建它们的会话结束
  • AND no other session is referring to them 没有其他会话指的是他们

This command can be handy to see which temporary tables exist: 此命令可以方便地查看存在哪些临时表:

select TABLE_NAME from tempdb.information_schema.tables 

And this is handy to drop temporary tables if you're not sure they exist: 如果您不确定它们是否存在,这对于删除临时表非常方便:

if object_id('tempdb..#SoTest') is not null drop table #SoTest

See this MSDN article for more information. 有关更多信息,请参阅此MSDN文章

The temporary table will be accesible to the instance of the procedure that creates it 临时表将可访问创建它的过程的实例

The following script 以下脚本

Exec ('Select 1 as col Into #Temp Select * From #Temp')
Exec ('Select 2 as col Into #Temp Select * From #Temp')

Returns 返回

Col
1

Col
2

Not

Col
1
2

Or an error because the table already exists. 或者因为表已经存在而出错。

The temporary table will also be accesible by any 'child' procedures that the initial procedure runs as well. 临时表也可以由初始过程运行的任何“子”程序访问。

下面的文章可能有所帮助:“如何在存储过程之间共享数据” http://www.sommarskog.se/share_data.html

You might also think about using table variables. 您可能还会考虑使用表变量。 They have a very well-defined scope, and they are sometimes faster than their temporary table counterparts. 它们具有非常明确的范围,并且它们有时比它们的临时表格更快。 The only problem with table variables is that they cannot be indexed, so some performance could be lost despite their nature. 表变量的唯一问题是它们不能被索引,因此尽管性质可能会丢失一些性能。 Check here for some more information on the subject. 点击此处查看有关此主题的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM