Re: Conditional test

From: "steve boyle" <boylesa(at)dial(dot)pipex(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Conditional test
Date: 2002-01-07 18:46:49
Message-ID: a1cqfm$174q$1@news.tht.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Rick, try the following plpgsql function it should do what you need:

create function f_dropTableIfExists (text) returns boolean as '
declare
p_tablename alias for $1;
temp_name text;
begin
select tablename from pg_tables into temp_name where tablename =
p_tablename;
if not found then
raise notice ''Table % not found.'', p_tablename;
return false;
else
execute ''drop table '' || p_tablename;
raise notice ''Table % dropped.'', p_tablename;
return true;
end if;
end ; '
language 'plpgsql';

you will need to 'Select f_dropTableIfExists([TableName]);' to execute the
function.

FAIK the main difference carrying out the operation this way rather than
just DROP TABLE is to avoid an ERROR being raised if the object does not
exist.

Hih

Steve Boyle

PS. If anyone knows a better way of doing this pls post

"Rick Dayao" <rickd(at)open-works(dot)com> wrote in message
news:4eee2f8d(dot)0112271854(dot)276c1152(at)posting(dot)google(dot)com(dot)(dot)(dot)
> I am trying to solve this problem:
>
> If a table exists, drop the table, otherwise continue processing.
>
> For example, if table X exists, then drop table X. How would I do
> this in SQL? I can query the pg_tables table to see which tables
> exist, but I'm looking for an If-Else construct in SQL.
>
> Thanks in advance,
>
> Rick

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Bax 2002-01-07 22:08:28 Re: simple? join
Previous Message Peter Eisentraut 2002-01-07 18:10:02 Re: Which date/paper pairs are NOT represented?