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 > Sybase > problem in SQL script to drop tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-02, 02:50
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 Sybase?
Thanks,
Siddharth
Reply With Quote
  #2 (permalink)  
Old 01-14-02, 08:24
MattR MattR is offline
Registered User
 
Join Date: Mar 2001
Location: Lexington, KY
Posts: 606
Sybase doesn't require you to jump through hoops and "catch" the error code. Seems kludgy. In T-SQL, you can do something like this:

Code:
if exists( SELECT * 
             FROM sysobjects 
            WHERE ID = object_id( 'table_name' ) ) 
	DROP TABLE table_name
I think the object_id is indexed in sysobjects whereas the name is not, although I may be wrong (runs off to check the sp_help sysobjects output ).

Ok, I see there's (at least in 12.5) an index upon id and a composite index upon name, uid. So either query will work fine.

edit: I see you found the answer in MS SQL Forums as well.

__________________
Thanks,

Matt
Reply With Quote
  #3 (permalink)  
Old 01-15-02, 03:02
siddharth siddharth is offline
Registered User
 
Join Date: Jan 2002
Posts: 13
Hi,
Sorry to be posting this on this forum, but I would just like to know if such a statement is possible in Oracle and DB2 also?

i.e. statement of the type

if exists( SELECT *
FROM sysobjects
WHERE ID = object_id( 'table_name' ) )
DROP TABLE table_name

Thanks and regards,
Siddharth.
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