473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SQL Temporary Table Concurrency

We have a few existing stored procedures which create a Global Temporary
Table (##), do some work on the table and then delete the table.

The issue we have is that if our Server application is using these is
running and then our Web application fires off, the Temp table is getting
deleted.

My thought was to change the Global Temporary Table to a local temporary
table (# instead of ##) to minimize the amount of change to these
procedures. My question though is in reference to the ASP.NET Web
application and whether the issue with the global table would still be an
issue with the local table.

I wasn't sure how connection pooling would play into this. I know that a
local temporary table is created for each connection and can only be
reference by that connection but i was wondering if in the ASP.NET
application is that if i call the procedure by two separate users in two
separate sessions if it would use the same connection and thus share the
same local temporary table.
Nov 18 '05 #1
5 2980

"Adam W. Saxton" <as*****@interactiontech.comNOSPAM> wrote in message
news:eV**************@TK2MSFTNGP09.phx.gbl...
We have a few existing stored procedures which create a Global Temporary
Table (##), do some work on the table and then delete the table.

The issue we have is that if our Server application is using these is
running and then our Web application fires off, the Temp table is getting
deleted.

My thought was to change the Global Temporary Table to a local temporary
table (# instead of ##) to minimize the amount of change to these
procedures. My question though is in reference to the ASP.NET Web
application and whether the issue with the global table would still be an
issue with the local table.

I wasn't sure how connection pooling would play into this. I know that a
local temporary table is created for each connection and can only be
reference by that connection but i was wondering if in the ASP.NET
application is that if i call the procedure by two separate users in two
separate sessions if it would use the same connection and thus share the
same local temporary table.


Each user will have exclusive access to the session so long as they hold the
Connection open.

So no user will see another's data, but you might see what another user left
in the temporary table.

So if the clients did

CREATE TABLE #FOO(I INT)
INSERT INTO #FOO(I) VALUES (1)

Then the second client would get an error trying to create #FOO because it
already existed.
But if each client runs

if not object_id('tempdb..#FOO') is null DROP TABLE #FOO
CREATE TABLE #FOO(I INT)
INSERT INTO #FOO(I) VALUES (1)

It's ok. You just have to account for other users leaving the temporary
table there.

David
Nov 18 '05 #2
That's what worries me.

If User A creates #FOO and then User B comes in before User A is done would
User B see some data that User A placed in #Foo and also if User A Drops the
table before User B is done, would User B get an error saying that #Foo does
not exists which i would think that would be true, and that is what was
happening with the Global table.
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:Ov**************@TK2MSFTNGP10.phx.gbl...

"Adam W. Saxton" <as*****@interactiontech.comNOSPAM> wrote in message
news:eV**************@TK2MSFTNGP09.phx.gbl...
We have a few existing stored procedures which create a Global Temporary
Table (##), do some work on the table and then delete the table.

The issue we have is that if our Server application is using these is
running and then our Web application fires off, the Temp table is getting deleted.

My thought was to change the Global Temporary Table to a local temporary
table (# instead of ##) to minimize the amount of change to these
procedures. My question though is in reference to the ASP.NET Web
application and whether the issue with the global table would still be an issue with the local table.

I wasn't sure how connection pooling would play into this. I know that a local temporary table is created for each connection and can only be
reference by that connection but i was wondering if in the ASP.NET
application is that if i call the procedure by two separate users in two
separate sessions if it would use the same connection and thus share the
same local temporary table.

Each user will have exclusive access to the session so long as they hold

the Connection open.

So no user will see another's data, but you might see what another user left in the temporary table.

So if the clients did

CREATE TABLE #FOO(I INT)
INSERT INTO #FOO(I) VALUES (1)

Then the second client would get an error trying to create #FOO because it
already existed.
But if each client runs

if not object_id('tempdb..#FOO') is null DROP TABLE #FOO
CREATE TABLE #FOO(I INT)
INSERT INTO #FOO(I) VALUES (1)

It's ok. You just have to account for other users leaving the temporary
table there.

David

Nov 18 '05 #3

"Adam W. Saxton" <as*****@interactiontech.comNOSPAM> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
That's what worries me.

If User A creates #FOO and then User B comes in before User A is done would User B see some data that User A placed in #Foo and also if User A Drops the table before User B is done, would User B get an error saying that #Foo does not exists
No. That would not happen. There will be a seperate #FOO for each session.
which i would think that would be true, and that is what was
happening with the Global table.


That is the difference between regular temporary tables (#) and global
temporary tables (##).

David
Nov 18 '05 #4
this is not correct.

#temp tables are tied to a spid (sqlconnection). they live until the spid
dies or the spid drops the table.

if you use connection pooling and don't create and drop the #temp table in
one batch (execute), then you can have the problem of two users accessing
the same #temp table (or one deleteing before the other is done).

sqlserver currently has no knowledge of connectiion pooling, future
versions, may impliment a connection reset, so cleanup can be done when the
connection is returned to the pool. this would mean the #temp table would
disappear as soon a conection went into the pool, even on the same page
request.

you can also safely create the #temp in a stored proc, becuase the sp will
delete it at exit, so its never shared.
-- bruce (sqlwork.com)
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:#c**************@tk2msftngp13.phx.gbl...

"Adam W. Saxton" <as*****@interactiontech.comNOSPAM> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
That's what worries me.

If User A creates #FOO and then User B comes in before User A is done would
User B see some data that User A placed in #Foo and also if User A Drops

the
table before User B is done, would User B get an error saying that #Foo

does
not exists


No. That would not happen. There will be a seperate #FOO for each

session.
which i would think that would be true, and that is what was
happening with the Global table.


That is the difference between regular temporary tables (#) and global
temporary tables (##).

David

Nov 18 '05 #5

"bruce barker" <no***********@safeco.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
this is not correct.

#temp tables are tied to a spid (sqlconnection). they live until the spid
dies or the spid drops the table.

if you use connection pooling and don't create and drop the #temp table in
one batch (execute), then you can have the problem of two users accessing
the same #temp table (or one deleteing before the other is done).


Not quite. You are guaranteed to be accessing the same temp table across
batches, so long as you are using the same SqlConnection object. When you
..Close the SqlConnection, the connection will return to the pool, and a
later user can retrieve that connection and "see" the same temp table. But
as I explained, you just have to account for the possibility that the temp
table may already exist in the session.

So it is safe to:
1. Open a "new" connection.
2. Drop the temp table (if it exists)
3. Create the temp table
4. Use the temp table in any number of batches
4a. (best practice) drop the temp table.
5. Close the connection
David

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Corrine | last post by:
Hi, I am creating a global temporary table that is session-specific. I insert a BLOB into this table, and then select the BLOB from this table into a ResultSet. The ResultSet sees this BLOB...
5
by: Jim Garrison | last post by:
Scenario: 1) Create a GLOBAL TEMPORARY table and populate it with one (1) row. 2) Join that table to another with about 1 million rows. The join condition selects a few hundred rows. ...
2
by: Calista | last post by:
I have a stored procedure that creates a temporary table. If two people (using the same account) execute the same stored procedure at the same time, will they overwrite / access the temporary...
4
by: gonzal | last post by:
Hi Dose any body know why a temporary table gets deleted after querying it the first time (using SELECT INTO)? When I run the code bellow I'm getting an error message when open the temp table...
11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
1
by: Mike L. Bell | last post by:
Query: update table1 t1 set end_time = ( select end_time from table2 t2 where t2.key1 = t1.key1 and t2.key2 = t1.key2 ) where exists
7
by: Larry | last post by:
Hi, I have unbelievable problems just to save a record! I make an input to a record in a subform, which has a temporary table as its recordsource. When I am done, and want to save the...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
3
by: Chris | last post by:
All I am cross-posting, as I'm not sure if this is an issue for the data layer or the application layer. If this is unacceptable, can someone let me know so that I don't do this in future. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.