Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > DB2 > problem in SQL script to drop tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-02, 03:51
siddharth siddharth is offline
Registered User
 
Join Date: Jan 2002
Posts: 13
problem in SQL script to drop tables

Hi,

How can we write a SQL statement to drop a table. The drop statement should not generate any error if the table doesn't exists. I have achieved the same in Oracle by catching the 'table not found exception'

The code I wrote is as follows:

CREATE PROCEDURE drop_table(pTableName VARCHAR2)
IS
vCommand VARCHAR2(1024);
table_not_exist EXCEPTION;
PRAGMA EXCEPTION_INIT(table_not_exist,-942);
BEGIN
vCommand := 'DROP TABLE ' || pTableName || ' CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE vCommand;
EXCEPTION
WHEN table_not_exist THEN
NULL;
WHEN OTHERS THEN
RAISE;
END drop_table;

Can I do something equivalent in DB2?
Thanks,
Siddharth
Reply With Quote
  #2 (permalink)  
Old 06-24-04, 02:38
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Use of if exists in droping tables if they exists. IS it possible in Db2?

IS it possible to drop and create a table in sql server style?
I mean to do somethoing like that

if exists(select * from sysobjects where name = table_name and ...)
drop table table_name
else
create table table_name

In Db2 is it possible to do something similar?
Reply With Quote
  #3 (permalink)  
Old 06-24-04, 04:54
brat4 brat4 is offline
Registered User
 
Join Date: Apr 2003
Location: Singapore
Posts: 59
Hi,
I have successfully used the following to continue handling in the procedure when no rows are found. I suspect if u can find out th SQLSTATE for dropping a nonexistant table, you can use the below. Do let me know if it works. To be honest I have not tried it yet.

P.N: 02000 is SQLSTATE for row not found when a sql query returns no rows


P1: Begin
DECLARE MASTER_NOT_FOUND CONDITION for SQLSTATE '02000';

DECLARE CONTINUE HANDLER for MASTER_NOT_FOUND SET MASTER_EXCEPTION_TMP='T';

<the sql to drop the table>

IF MASTER_EXCEPTION_TMP='T' THEN
<do some processing>
ELSE
<do some processing>

ENd IF
END P1

The variable MASTER_EXCEPTION_TMP gets a value of 'T' if the <sql query> returns no rows.

HTH
Cheers
Brat
Reply With Quote
  #4 (permalink)  
Old 06-24-04, 06:34
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 3,329
As mentioned above, you need to use "continue handling" in a stored procedure if you want to intercept and act on specific error return codes in your procedure. Otherwise DB2 will exit the routine when it thinks a fatal error has occured.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
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

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