X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id CEB4C30E64F for ; Fri, 20 Jun 2003 15:50:59 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 61644-07 for ; Fri, 20 Jun 2003 12:50:45 -0300 (ADT) Received: from gatekeeper.gmanetwork.com (unknown [202.61.78.68]) by svr1.postgresql.org (Postfix) with ESMTP id E75AB30DBA3 for ; Fri, 20 Jun 2003 12:50:43 -0300 (ADT) Received: from gmanewmedia.com (mailhost.gmanmi.tv [192.168.6.3]) by gatekeeper.gmanetwork.com (8.11.0/8.11.0) with ESMTP id h5KFofk10895; Fri, 20 Jun 2003 23:50:41 +0800 Received: from CMPMEL ([192.168.6.27]) by gmanewmedia.com (8.11.1/) with SMTP id h5KFeSQ07959; Fri, 20 Jun 2003 23:40:31 +0800 (PHT) (envelope-from mel@gmanmi.tv) X-RAV-AntiVirus: This e-mail has been scanned for viruses on host: gmanewmedia.com Reply-To: From: "Mel Jamero" To: "'Erwin Ambrosch'" Cc: Subject: Re: pgsql function doesn't work Date: Sat, 21 Jun 2003 00:12:10 +0800 Message-ID: <000301c33746$b4a8ddf0$1b06a8c0@CMPMEL> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 In-Reply-To: <20030618102030.W33406-100000@megazone23.bigpanda.com> X-Archive-Number: 200306/273 X-Sequence-Number: 9264 taking queue from Stephan, this would achieve what you want to do though it i don't find it too 'neat': DROP FUNCTION drop_table_if_exists(TEXT); CREATE FUNCTION drop_table_if_exists(TEXT) RETURNS BOOLEAN AS ' DECLARE str_tmp text; BEGIN SELECT INTO str_tmp tablename FROM pg_tables WHERE tablename = $1; IF FOUND THEN EXECUTE ''DROP TABLE'' || '' '' || $1; RETURN true; END IF; RETURN false; END;' LANGUAGE 'plpgsql'; HTH. -- Mel Jamero -----Original Message----- From: pgsql-admin-owner@postgresql.org [mailto:pgsql-admin-owner@postgresql.org]On Behalf Of Stephan Szabo Sent: Thursday, June 19, 2003 1:24 AM To: Erwin Ambrosch Cc: pgsql-admin@postgresql.org Subject: Re: [ADMIN] pgsql function doesn't work On Wed, 18 Jun 2003, Erwin Ambrosch wrote: > Hi, > > the function bellow is created successfully, but executing it forces the > following error message: > > WARNING: Error occurred while executing PL/pgSQL function > drop_table_if_exists > WARNING: line 5 at select into variables > ERROR: parser: parse error at or near "$1" at character 34 > > > CREATE OR REPLACE FUNCTION drop_table_if_exists(TEXT) RETURNS BOOLEAN AS ' > DECLARE > rec RECORD; > BEGIN > > SELECT INTO rec off_mitarbeiter_id FROM $1; You can't do this in plpgsql without some form of EXECUTE. > IF FOUND THEN > EXECUTE ''DROP TABLE'' || '' '' || $1; > RETURN true; > END IF; > > RETURN false; > > END;' > LANGUAGE 'plpgsql'; However, AFAICS this will error if the table doesn't exist (selecting from a non-existant table) rather than do nothing which I'm guessing was the point since otherwise you could do the drop without the function. You might want to consider querying the system catalogs instead. ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend