Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g07Iw6B51833 for ; Mon, 7 Jan 2002 13:58:06 -0500 (EST) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id g07IlKg40093 for pgsql-sql@postgresql.org; Mon, 7 Jan 2002 13:47:20 -0500 (EST) (envelope-from news) From: "steve boyle" X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Conditional test Date: Mon, 7 Jan 2002 18:46:49 -0000 Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 48 Message-ID: References: <4eee2f8d.0112271854.276c1152@posting.google.com> X-Complaints-To: news@hub.org X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 To: pgsql-sql@postgresql.org X-Archive-Number: 200201/126 X-Sequence-Number: 6214 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" wrote in message news:4eee2f8d.0112271854.276c1152@posting.google.com... > 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