Re: How to check if a table exists from functions.

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Mikael Kjellstrm <mikael(dot)kjellstrom(at)_no_spam_flygtaxi(dot)se>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to check if a table exists from functions.
Date: 2001-06-20 21:24:50
Message-ID: Pine.BSF.4.21.0106201421270.99731-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Tue, 19 Jun 2001, Mikael Kjellstrm wrote:

>
> Maybe I am doing this the wrong way, but how do I check if a table exist?
>
> This works from psql:
>
> select
> relname
> from
> pg_class
> where
> relname = 'tablename'
>
>
> But if I try to do the same from a PL/PGSQL stored procedure I get the
> following error message:
>
> ERROR: unexpected SELECT query in exec_stmt_execsql()
>
> Isn't select's allowed from stored procedures?

You'll want to use something like:
declare
foo name;
begin
...
select relname INTO foo from pg_class where
relname=''tablename'';
...
end;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ari Nepon 2001-06-20 21:34:30 Help with a double left join
Previous Message sbelow 2001-06-20 19:37:39 firehouse