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 > If table not exists create else delete from

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-02-10, 10:26
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
Question If table not exists create else delete from

I am looking to check to see if a table exists. If it does, I want to delete every row in it. If the table does not exist, I want to create the table. This is all preparation to use the table.

When I put the pieces together, it does not work. As three separate statements, they work assuming that the table is created or is not created as needed. Together they fail. Any ideas? I must have something rather simple wrong.
Code:
IF 
    ( 
        ( 
        SELECT 
            COUNT(*) 
        FROM 
            SYSCAT.TABLES 
        WHERE 
            TABSCHEMA   = 'TEST_SCHEMA' 
            AND TABNAME = 'TEST_TABLE' 
        ) 
        = 0 
    ) 
    BEGIN 
        CREATE 
            TABLE TEST_SCHEMA.TEST_TABLE 
            ( 
                ID INT 
            ) 
        END 
    ELSE 
        BEGIN 
            DELETE 
            FROM 
                BISDCUST.ACE_GRADES_AUSTIN 
            END
            ;
Reply With Quote
  #2 (permalink)  
Old 06-02-10, 10:31
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What database version and OS are you using? What is the error you are getting?

Andy
Reply With Quote
  #3 (permalink)  
Old 06-02-10, 11:38
dbzTHEdinosaur dbzTHEdinosaur is offline
Registered User
 
Join Date: Jun 2007
Location: germany
Posts: 96
why don't you delete the table, regardless, ignore the return if no table exists
and
then create the table?
__________________
Dick Brenholtz, Ami in Deutschland
Reply With Quote
  #4 (permalink)  
Old 06-03-10, 10:23
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
Quote:
Originally Posted by dbzTHEdinosaur View Post
why don't you delete the table, regardless, ignore the return if no table exists
and
then create the table?
The create takes longer than delete on our system. I would prefer the faster solution.
Reply With Quote
  #5 (permalink)  
Old 06-03-10, 10:58
dbzTHEdinosaur dbzTHEdinosaur is offline
Registered User
 
Join Date: Jun 2007
Location: germany
Posts: 96
Quote:
The create takes longer than delete on our system. I would prefer the faster solution.
that makes a lot of sense.
__________________
Dick Brenholtz, Ami in Deutschland
Reply With Quote
  #6 (permalink)  
Old 06-03-10, 11:48
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
Anyone know of a try/catch equivalent on db2?

I googled sql try catch, but the result is for mssql. Pretty cool actually. I tried it on our mssql box, but the db2 box didn't like it.

mssql:
Code:
BEGIN TRY
    CREATE TABLE myschema.DBO.TRYTHIS
    (
        ID VARCHAR(8)
    )
END TRY
BEGIN CATCH
    DROP TABLE myschema.DBO.TRYTHIS
END CATCH
A solution like this would speed many of our queries because sometimes subqueries need to be run twice such as
Code:
with sq as
(select value from sourcetable where cond)
select case when sq is null then 0 else sq from sourcetable2
if the query could be rewritten as
Code:
with sq as
(select value from sourcetable where cond)
select begin try sq end try begin catch 0 end catch from sourcetable2
The query would execute once instead of twice for every row.
Reply With Quote
  #7 (permalink)  
Old 06-03-10, 12:17
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by jkuyken View Post
Anyone know of a try/catch equivalent on db2?
Check out condition handlers in the DB2 manual.
Reply With Quote
  #8 (permalink)  
Old 06-03-10, 23:53
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
That must be part of my problem

No manual! I will look into acquiring one. :-)
Reply With Quote
  #9 (permalink)  
Old 06-04-10, 10:28
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by jkuyken View Post
No manual! I will look into acquiring one. :-)
Consider it acquired: IBM DB2 9.7 for Linux, UNIX and Windows Information Center
Reply With Quote
  #10 (permalink)  
Old 07-20-10, 16:56
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
Reply With Quote
Reply

Tags
create table, delete from, if not exists

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