Re: Re: Re: Test for existence of Table - Mailing list pgsql-general

From Ed Loehr
Subject Re: Re: Re: Test for existence of Table
Date
Msg-id 3A561E7A.2427E623@austin.rr.com
Whole thread Raw
In response to RE: Re: Test for existence of Table  ("Craig L. Ching" <cching@mqsoftware.com>)
List pgsql-general
Gregory Wood wrote:
>
> What would be nice is if there were a way to only DROP a table if it exists.
> But I would consider this to be rather low priority.

This might help...

CREATE FUNCTION table_exists(TEXT) RETURNS BOOLEAN AS
'DECLARE
        tablename ALIAS FOR $1;
        temp RECORD;
BEGIN
    SELECT INTO temp *
    FROM pg_class c
    WHERE c.relname = tablename
      AND c.relkind = ''r'';

    if found then
        return ''t''::BOOLEAN;
    else
        return ''f''::BOOLEAN;
    end if;
END;'
LANGUAGE 'plpgsql';

-- test table
CREATE TABLE realtable (id INTEGER);

-- test example
SELECT table_exists('realtable'::TEXT);
SELECT table_exists('faketable'::TEXT);

-- clean up
DROP TABLE realtable;
DROP FUNCTION table_exists(TEXT);

It'd be even nicer if you could drop the table from within the PL/pgSQL
function, but I found that does not work in 7.0.0.

pgsql-general by date:

Previous
From: Alex Pilosov
Date:
Subject: Re: running pgsql 7 under Jail'ed virtual machine on FreeBSD 4.2
Next
From: Scott Teglasi
Date:
Subject: Access 2000 and PostgreSQL: Record Editing Problems