Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Check for a table, then drop

Re: Check for a table, then drop

From: Paul722532 <paul722532_at_aol.com>
Date: 1998/03/04
Message-ID: <19980304225301.RAA27827@ladder02.news.aol.com>#1/1

Hi,

I don't really understand why you want to do this.

I would usually simply drop the table, if it exists it gets dropped, if it doesn't then it errors. As far as I know you can't use DDL in a PL/SQL block unless it is dynamic. If you wish to check if the table exists then you can query all_tables, if it exists you can then drop it. Another solution would be to execute the drop command and ignore the error that is generated if the table exists.

Whatever, the below procedure will check if the table exists in all_tables if it does then it will create and execute a drop statement dynamically. If the table does not exist then the no_data_found exception raised is ignored. You could remove the select statement from this procedure and ignore the table does not exist message from the drop statement.

CREATE OR REPLACE PROCEDURE droptab (
  p_tabname all_tables.table_name%TYPE) AS BEGIN
  DECLARE

    v_cursor NUMBER;
    v_check NUMBER;
    v_numrows NUMBER;
    v_dropstring varchar2(100);

  BEGIN
/*Check if table exists if no data returned goes to exception*/
    SELECT 1
      INTO v_check 
      FROM all_tables 
      WHERE table_name = p_tabname;    

/* Drop Table - dynamically create drop statement*/
    v_cursor := DBMS_SQL.OPEN_CURSOR;
    v_dropstring := 'DROP TABLE '||p_tabname;     DBMS_SQL.PARSE(v_cursor, v_dropstring, DBMS_SQL.V7);     v_numrows := DBMS_SQL.EXECUTE(v_cursor);     DBMS_SQL.CLOSE_CURSOR(v_cursor);

  EXCEPTION /*If an exception other than no_data_found then raise */     WHEN NO_DATA_FOUND THEN
      NULL;
    WHEN OTHERS THEN
      RAISE;
  END;
END droptab;

I Hope somewhere in here is what you wanted to know otherwise I'll feel really stupid!

Cheers,

Paul Received on Wed Mar 04 1998 - 00:00:00 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US