Are temporary tables thread-safe?

前端 未结 9 550
清酒与你
清酒与你 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: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.

提交回复
热议问题