If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Check existence of temporary table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-07, 12:02
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
Check existence of temporary table

How can I check using SQL (in a SQL stored procedure) if a temporary table exists?
Reply With Quote
  #2 (permalink)  
Old 09-27-07, 12:37
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
are you refering GTT
i suspect there would be any info about them on system level tables as they are valid only for session
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #3 (permalink)  
Old 09-27-07, 12:40
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You can set up an appropriate CONDITION HANDLER for SQLSTATE 42704.

Then you can just issue a select on the temp table.

Andy
Reply With Quote
  #4 (permalink)  
Old 09-27-07, 13:24
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
Quote:
Originally Posted by rahul_s80
are you refering GTT
i suspect there would be any info about them on system level tables as they are valid only for session
yes, it is a global temporary table. I have been browsing though the sys tables and couldn't find anything like it. However, maybe I missunderstand the concept of session but I do connect from a .NET client to call a stored procedure that creates a table and it seems that the table is persistent between calls.
Is there any way that I can check if it exists already so that I don't try to redeclare it? (as that fails).
Also, is there anything wrong in this pattern?
Basically I declare a temp table to create some data, open a cursor with return on it (as I need to return that data to the caller) and therefore I cannot drop the table (as I have an open cursor on it). I thought it would dissapear in between calls but it doesn't.
Reply With Quote
  #5 (permalink)  
Old 09-27-07, 13:25
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
Quote:
Originally Posted by ARWinner
You can set up an appropriate CONDITION HANDLER for SQLSTATE 42704.

Then you can just issue a select on the temp table.

Andy
I actually need to know of its existence when I declare it, not when I select from it.
Reply With Quote
  #6 (permalink)  
Old 09-27-07, 13:31
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Your first post did not explain why you wanted to check if it existed.

When you declare it, you can use the clause "WITH REPLACE' which will either create a new one if it does not exist or replace the definition if it exists. All Global Temp Tables will persist until it is either explicitly dropped (DROP TABLE SESSION.MyTempTable), or the connection is terminated.

Andy
Reply With Quote
  #7 (permalink)  
Old 09-27-07, 13:33
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
that makes sense.
Thanks
Reply With Quote
  #8 (permalink)  
Old 09-29-07, 12:54
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Global temp tables are "declared", which indicates that information about those tables is not stored in the system catalog. Things are different with "created" temp tables (which DB2 LUW does not yet provide).
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 09-29-07, 14:24
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by iskander
I actually need to know of its existence when I declare it, not when I select from it.
Still, you could first select from it, and it that *fails* all is fine (since the table does not exist) but if it *succeeds* you know that the declaration will fail.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On