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 > Drop Table in If-Else loop

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-28-09, 06:03
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
Drop Table in If-Else loop

hi all,
my aim is check if object exists then drop him
i've query:
--#SET TERMINATOR @
BEGIN ATOMIC
IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'TBL_NAME) THEN DROP TABLE 'TBL_NAME';
END IF;
END@

The error is:
DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command.
SQL0104N An unexpected token "drop table" was found following "= 'tbl_name')
THEN ". Expected tokens may include: "<signal_stmt_head>". LINE NUMBER=3.
SQLSTATE=42601
Reply With Quote
  #2 (permalink)  
Old 07-28-09, 06:34
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
You cannot use a variable in a DDL .. YOu have to use dynamic sql

An alternative is to drop and ignore the warning if the table does not exists ..
Pseudocode:
Code:
CREATE PROCEDURE DROPT

P1: BEGIN
-- Declare variable
DECLARE v_sqlstate_test CHAR(5); 
DECLARE v_sqlcode_test INT;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLCODE INT DEFAULT 0;
DECLARE v_TABLE_NOT_FOUND INT DEFAULT 0;
DECLARE c_TABLE_NOT_FOUND CONDITION FOR SQLSTATE '56098';

DECLARE CONTINUE HANDLER FOR c_TABLE_NOT_FOUND 
SET v_TABLE_NOT_FOUND = 1; 

DROP TABLE T1 ; 
IF (v_TABLE_NOT_FOUND ==1 ) tHEN "GO TO NEXT STEP" ELSE "gO TO NEXT STE" 
END IF
END IF;
END P1
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 07-28-09, 06:59
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
Thank you for your quick answer.
Maybe can you tell me how can i print text in DB2 (like dbms_output in oracle)
Reply With Quote
  #4 (permalink)  
Old 07-28-09, 07:05
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Unless you are using DB2 9.7 - you have to use udfs ..

Making Operating System Calls from SQL

The above article (by Stolze, who freqents this forum) gives you the examples

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 07-28-09, 09:43
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
hi,
I tied to run your code, but didn't succeed
Can you explain your code
thanks
Reply With Quote
  #6 (permalink)  
Old 07-28-09, 11:07
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
What's wrong in this query ?

--#SET TERMINATOR @
BEGIN ATOMIC
--DECLARE TXT VARCHAR(100);
IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'QA_TBL') THEN
SELECT 'DROP TABLE ' || NAME AS TXT FROM sysibm.systables WHERE name = 'QA_TBL' ;
EXECUTE IMMEDIATE = TXT;
END IF;
END @
Reply With Quote
  #7 (permalink)  
Old 07-28-09, 12:00
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
ELECT 'DROP TABLE ' || NAME INTO TXT FROM sysibm.systables WHERE name = 'QA_TBL' ;
EXECUTE IMMEDIATE TXT;
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #8 (permalink)  
Old 07-29-09, 02:28
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
SELECT 'DROP TABLE ' || NAME INTO TXT FROM sysibm.systables WHERE name = 'QA_TBL' ;

wrong syntax
Reply With Quote
  #9 (permalink)  
Old 07-29-09, 04:22
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
I know !! sorry, i knew ...

__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #10 (permalink)  
Old 07-29-09, 07:09
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
Quote:
Originally Posted by sathyaram_s
I know !! sorry, i knew ...

so what can i do ?
Reply With Quote
  #11 (permalink)  
Old 07-29-09, 08:23
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Read the error message and the corresponding explanation in the manual ..

If you have a clarification, post here
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #12 (permalink)  
Old 07-30-09, 07:47
borusik75 borusik75 is offline
Registered User
 
Join Date: Jul 2009
Posts: 7
I've solution
1. Create SP(IN TBL VARCHAR(100)) that drop table
2. IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'QA_TBL') THEN CALL SP(TBL)
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