Are temporary tables thread-safe?

前端 未结 9 510
清酒与你
清酒与你 2021-02-05 00:10

I\'m using SQL Server 2000, and many of the stored procedures it use temp tables extensively. The database has a lot of traffic, and I\'m concerned about the thread-safety of cr

相关标签:
9条回答
  • 2021-02-05 00:39

    unless you use two pound signs ##temp the temp table will be local and only exists for that local connection to the user

    0 讨论(0)
  • 2021-02-05 00:41

    For the first case, no, it is not possible, because #temp is a local temporary table, and therefore not visible to other connections (it's assumed that your users are using separate database connections). The temp table name is aliased to a random name that is generated and you reference that when you reference your local temp table.

    In your case, since you are creating a local temp table in a stored procedure, that temp table will be dropped when the scope of the procedure is exited (see the "remarks section").

    A local temporary table created in a stored procedure is dropped automatically when the stored procedure completes. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process which called the stored procedure that created the table.

    For the second case, yes, you will get this error, because the table already exists, and the table lasts for as long as the connection does. If this is the case, then I recommend you check for the existence of the table before you try to create it.

    0 讨论(0)
  • 2021-02-05 00:46

    Local-scope temp tables (with a single #) are created with an identifier at the end of them that makes them unique; multiple callers (even with the same login) should never overlap.

    (Try it: create the same temp table from two connections and same login. Then query tempdb.dbo.sysobjects to see the actual tables created...)

    0 讨论(0)
提交回复
热议问题