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: PLSQL: how to test if a table exists?

Re: PLSQL: how to test if a table exists?

From: Chris ( Val ) <chrisval_at_bigpond.com.au>
Date: 26 Jun 2005 16:01:12 -0700
Message-ID: <1119826499.581654.314110@g49g2000cwa.googlegroups.com>

deanbrown3d wrote:
> if (select table_name from all_tables where owner = 'me' and table_name
> = 'MYTABLE') is null........

This seems like a very common query to perform.

I would probably wrap it up in a function, and place it in my own package for later use:

CREATE OR REPLACE FUNCTION
          tableExists( O VARCHAR2, T VARCHAR2 ) RETURN VARCHAR2 AS

v_TableName all_tables.table_name%type;

BEGIN   SELECT table_name INTO v_TableName
    FROM all_tables
    WHERE owner = UPPER( O )
    AND table_name = UPPER( T );

  RETURN 'Table ' || '"' || T || '"' || ' Exists';

  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      RETURN 'Table ' || '"' || T || '"' || ' Does Not Exist';     WHEN OTHERS THEN
      RETURN 'Unknown error occurred';

END tableExists;
/

Of course, you could return a boolean type or even a char(1) to signify 'T', 'F' or 'U' if you like.

Cheers,
Chris Val Received on Sun Jun 26 2005 - 18:01:12 CDT

Original text of this message

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