From: | Lee Kindness <lkindness(at)csl(dot)co(dot)uk> |
---|---|
To: | Jean-Christian Imbeault <totsubo2001(at)netscape(dot)net> |
Cc: | Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, pgsql-general(at)postgresql(dot)org |
Subject: | SQL: how to find if a table exists? |
Date: | 2002-09-09 14:41:54 |
Message-ID: | 15740.45874.945784.646631@kelvin.csl.co.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Look into the pg_class system table for a matching 'relame', the code
below can be installed as a plpgsql function to add a 'table_exists()'
function which returns boolean:
CREATE OR REPLACE FUNCTION table_exists(NAME) RETURNS BOOLEAN AS '
DECLARE
tab ALIAS FOR $1;
rec RECORD;
BEGIN
SELECT INTO rec *
FROM pg_class c
WHERE c.relname = tab;
IF NOT FOUND THEN
RETURN false;
ELSE
RETURN true;
END IF;
END;
' LANGUAGE 'plpgsql';
This actually matches index names too, but works for my uses... This
system table is documented at:
http://www.postgresql.org/idocs/index.php?catalog-pg-class.html
Lee.
Jean-Christian Imbeault writes:
> I need to programmatically create a table if it does not already exists.
>
> Is there an SQL statement that will allow me to query a DB to see if a
> table exists?
From | Date | Subject | |
---|---|---|---|
Next Message | Joe Murphy | 2002-09-09 14:43:42 | Re: Vacuum and indexes problem |
Previous Message | Oliver Elphick | 2002-09-09 14:38:40 | Re: starting with linux as another user... |