The two types of temporary tables, local and global, differ from each other in their names, their visibility, and their availability. Local temporary tables have a single number sign (#) as the first character of their names; they are visible only to the current connection for the user; and they are deleted when the user disconnects from instances of Microsoft® SQL Server™ 2000. Global temporary tables have two number signs (##) as the first characters of their names; they are visible to any user after they are created; and they are deleted when all users referencing the table disconnect from SQL Server.
For example, if you create a table named employees, the table can be used by any person who has the security permissions in the database to use it, until the table is deleted. If you create a local temporary table named #employees, you are the only person who can work with the table, and it is deleted when you disconnect. If you create a global temporary table named ##employees, any user in the database can work with this table. If no other user works with this table after you create it, the table is deleted when you disconnect. If another user works with the table after you create it, SQL Server deletes it when both of you disconnect.
上面是拷贝自sql server2000的book online
主要是为了这样一个问题,在我的模块中的一个存储过程中,我创建并使用了一个临时表#myTempTable.如果多个人(未知多少)使用一个login登陆到数据库操纵这个模块。会不会发生并发?
上面的E文描述,local的临时表是针对于current connection的。是不是并发问题sql server已经帮我们搞定了?
---------------------------------------------------------------
是的,不会有并发问题,local temporary table(#myTempTable)按连接命名,在数据库的名称是形如#myTempTable___________12345的,不同连接的临时表的名的后面部分是不一样的,这个你可以在TEMPDB的SYSOBJECTS表查到。
---------------------------------------------------------------
临时表
可以创建本地和全局临时表。本地临时表仅在当前会话中可见;全局临时表在所有会话中都可见。
本地临时表的名称前面有一个编号符 (#table_name),而全局临时表的名称前面有两个编号符 (##table_name)。
SQL 语句使用 CREATE TABLE 语句中为 table_name 指定的名称引用临时表:
CREATE TABLE #MyTempTable (cola INT PRIMARY KEY)
INSERT INTO #MyTempTable VALUES (1)
如果本地临时表由存储过程创建或由多个用户同时执行的应用程序创建,则 SQL Server 必须能够区分由不同用户创建的表。为此,SQL Server 在内部为每个本地临时表的表名追加一个数字后缀。存储在 tempdb 数据库的 sysobjects 表中的临时表,其全名由 CREATE TABLE 语句中指定的表名和系统生成的数字后缀组成。为了允许追加后缀,为本地临时表指定的表名 table_name 不能超过 116 个字符。
除非使用 DROP TABLE 语句显式除去临时表,否则临时表将在退出其作用域时由系统自动除去:
当存储过程完成时,将自动除去在存储过程中创建的本地临时表。由创建表的存储过程执行的所有嵌套存储过程都可以引用此表。但调用创建此表的存储过程的进程无法引用此表。
所有其它本地临时表在当前会话结束时自动除去。
全局临时表在创建此表的会话结束且其它任务停止对其引用时自动除去。任务与表之间的关联只在单个 Transact-SQL 语句的生存周期内保持。换言之,当创建全局临时表的会话结束时,最后一条引用此表的 Transact-SQL 语句完成后,将自动除去此表。
本地(local)表不会由于多个用户同时使用而发生并发.因为sql server会根据不不同连接生成不同的表名(SQL Server 必须能够区分由不同用户创建的表。为此,SQL Server 在内部为每个本地临时表的表名追加一个数字后缀。)
---------------------------------------------------------------
#开头的临时表只共当前连接使用(局部),##临时表可供全局使用,即他人可存取,由系统自动管理,所以没有并发问题.
所有与临时表的连接断开时,系统自动删除你所创建的临时表.
---------------------------------------------------------------
很深刻的问题!
我想会出问题,且不好解决并发性问题,
因此我尽量不使用#临时表,而使用##临时表,且对##临时表名进行处理
我的过程要求有一个计算机名称参数,临时表名称是根据计算机名称得出来的,以此来应对多用户的问题。看如下临时文件的命名:
select @sTmpWareA="tempdb..[##MARWareA"+ @ComputerName+"]"
if exists (select * from tempdb..sysobjects where id = object_id(@sTmpWareA) and type = "U")
begin
set @sTmpWareA="[##MARWareA"+ @ComputerName+"]"
exec( "drop table " +@sTmpWareA )
end
else
set @sTmpWareA="[##MARWareA"+ @ComputerName+"]"
@sTmpWareA 就是临时表的名称,过程中使用exec来操作,很笨拙,也很无奈,
---------------------------------------------------------------
建议:
1、如果用2000,在存储过程和触发器里,数据量不大的用表变量。
2、不要滥用全局临时表。
---------------------------------------------------------------
mornwoo(疾风之虫) 的担心是多余的,应该说,只要是不同连接,从来不会有并发问题。同一连接会出现“临时表已经存在”的错误。
---------------------------------------------------------------
关注
---------------------------------------------------------------
本地临时表仅在当前会话中可见,不会发生并发问题。
---------------------------------------------------------------
本地临时表仅在当前会话中可见,不会发生并发问题。
---------------------------------------------------------------
学习
---------------------------------------------------------------
to Yang_(扬帆破浪) :
请问全局临时表会有并发性问题吗?
---------------------------------------------------------------
向高手学习!
---------------------------------------------------------------
这个问题在今年1月份坛子上也讨论得比较热烈,于是我专门做了实验。
结果是:
1。在一个存储过程中创建本地临时表,利用它做插入数据然后求和的操作,当然存入的数据的随机的。使用存储过程的调试功能,跟踪下来,发现多会话的情况下,本地临时表互不干扰,跟sqlserver的帮助说法一样。每个会话创建的临时表表名的最后的数字不同。
2。在一个存储过程中创建全局临时表,做同样的操作。第一个会话创建后,第二个会话再创建报错,所以使用全局临时表时一般最好加上如果表不存在就创建,如果存在就直接操作。跟踪结果显示所有会话共用这个全局临时表。
给我的感觉是:本地和全局临时表跟本地和全局变量的表现形式一样。
---------------------------------------------------------------
to: mornwoo(疾风之虫)
会,不同连接不能同时创建同名的全局临时表。
---------------------------------------------------------------
有些应用必须使用全局临时表,就应该在表名上控制使得不重复!!
---------------------------------------------------------------
gz,学习
---------------------------------------------------------------
有些应用必须使用全局临时表,就应该在表名上控制使得不重复!!
---------------------------------------------------------------
感谢: Yang_(扬帆破浪),bluepower2008(蓝色力量)
---------------------------------------------------------------
up
---------------------------------------------------------------
学习了,