Home All Groups Group Topic Archive Search About

What is the "current session" for a local temp table?

Author
30 Mar 2006 9:10 PM
Bill
Using SS 2000. BOL says that local temporary tables are private to the
"current session". I assume this means to the connection. For example,
If my app is connected to SQL Server and runs a process that creates a
temp table and I then run a second instance of my app on the same PC
and log in as the same user and run the same process I assume that each
instance of my app will have its own temp table. Is this correct?

On the other hand, if my app attempts to run the process that creates
the local temp table twice at the same time in two different threads
there will be a collision because both threads will see the same
instance of the temp table. Is this correct?

Thanks.

--
..Bill.

Author
30 Mar 2006 9:24 PM
Geoff N. Hiten
Under both circumstances you would see two separate temp tables.  SQL has no
clue that two different connections belong to the same application.
Application threading models don't matter to SQL since all it sees are
connections.

From the updated BOL:
Temporary tables are automatically dropped when they go out of scope, unless
explicitly dropped using DROP TABLE:

  a.. 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.


  b.. All other local temporary tables are dropped automatically at the end
of the current session.
----- end excerpt

Most of the time "session" is the same as "connection" for this purpose.
One exception is when you use an EXECUTE command to run dynamic SQL.
Everything inside that command is a separate session, just like temp table
scoping within a stored procedure.

--
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP





Show quoteHide quote
"Bill" <n*@no.com> wrote in message
news:ecfqY5DVGHA.1564@TK2MSFTNGP11.phx.gbl...
> Using SS 2000. BOL says that local temporary tables are private to the
> "current session". I assume this means to the connection. For example,
> If my app is connected to SQL Server and runs a process that creates a
> temp table and I then run a second instance of my app on the same PC
> and log in as the same user and run the same process I assume that each
> instance of my app will have its own temp table. Is this correct?
>
> On the other hand, if my app attempts to run the process that creates
> the local temp table twice at the same time in two different threads
> there will be a collision because both threads will see the same
> instance of the temp table. Is this correct?
>
> Thanks.
>
> --
> .Bill.
Are all your drivers up to date? click for free checkup

Author
30 Mar 2006 9:37 PM
Mike Hodgson
Bill wrote:

Show quoteHide quote
>Using SS 2000. BOL says that local temporary tables are private to the
>"current session". I assume this means to the connection. For example,
>If my app is connected to SQL Server and runs a process that creates a
>temp table and I then run a second instance of my app on the same PC
>and log in as the same user and run the same process I assume that each
>instance of my app will have its own temp table. Is this correct?

>
Yes.

>On the other hand, if my app attempts to run the process that creates
>the local temp table twice at the same time in two different threads
>there will be a collision because both threads will see the same
>instance of the temp table. Is this correct?

>
The 2 different threads will most likely use separate connections to the
SQL server, in which case the temp tables will be created in different
sessions and so get different names in tempdb (as the session context is
included (invisibly...sort of) in the table name in tempdb).  However,
if the 2 threads manage to share a connection to the SQL server, for
example in a connection pooling scenario, then if you give the temp
tables the same name in both threads the SQL server will return an error
message to the second thread (that attempted to create the table) to the
affect that the table already exists.

Author
30 Mar 2006 10:31 PM
Erland Sommarskog
Mike Hodgson (e1mins***@gmail.com) writes:
> However, if the 2 threads manage to share a connection to the SQL
> server, for example in a connection pooling scenario, then if you give
> the temp tables the same name in both threads the SQL server will return
> an error message to the second thread (that attempted to create the
> table) to the affect that the table already exists.

Note that in normal connection pooling this would not happen. Each
thread would get a connection, which may be from the same pool, but
they would not get the same connection at the same time.

But if you declare your connection object on global level in your
app, and start to run multiple threads, that uses this connection
(which would be protected by a semaphore) this could happen. But
I don't think very many people would consider such design.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Bookmark and Share

Post Thread options