X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8126E3B30E8 for ; Thu, 23 Dec 2004 15:27:49 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 55676-08 for ; Thu, 23 Dec 2004 15:27:21 +0000 (GMT) Received: from gatefox.foxi.nl (foxi.xs4all.nl [194.109.246.192]) by svr1.postgresql.org (Postfix) with ESMTP id 518D53B30F9 for ; Thu, 23 Dec 2004 15:27:23 +0000 (GMT) Received: from techfox.foxi (techfox.foxi [192.168.0.79]) by gatefox.foxi.nl (8.10.2-20030922/8.10.2) with ESMTP id iBNFRKM18656; Thu, 23 Dec 2004 16:27:21 +0100 From: Frank van Vugt Organization: Foxi Consultants in Technology BV To: "Joost Kraaijeveld" Subject: Re: plpgsql howto question Date: Thu, 23 Dec 2004 16:27:20 +0100 User-Agent: KMail/1.5.4 References: In-Reply-To: Cc: MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200412231627.20577.ftm.van.vugt@foxi.nl> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.167 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200412/51 X-Sequence-Number: 5221 Hi Joost, > What is the correct way of doing this? As 'always', there's more than one way of doing this, for example - selecting into a var and returning that var - selecting into a record and returning the proper field of that record - return the proper values immediately - use 'plain' sql in combination with a case statement Some examples: CREATE OR REPLACE FUNCTION getcustomername(text, int4) RETURNS text AS ' DECLARE my_person TEXT; my_company RECORD; BEGIN IF $2 = 1 THEN SELECT INTO my_person lastname FROM person WHERE objectid = $1; RETURN my_person; ELSIF $2 = 2 THEN SELECT INTO my_company name1 from company WHERE objectid = $1; RETURN my_company.name1; END IF; END ' LANGUAGE 'plpgsql' VOLATILE; ***** CREATE OR REPLACE FUNCTION getcustomername(text, int4) RETURNS text AS ' DECLARE my_id ALIAS FOR $1; my_type ALIAS FOR $2; BEGIN IF my_type = 1 THEN RETURN my_person lastname FROM person WHERE objectid = my_id; ELSIF my_type = 2 THEN RETURN name1 from company WHERE objectid = my_id; END IF; END ' LANGUAGE 'plpgsql' VOLATILE; ***** SELECT CASE WHEN type = 1 THEN (SELECT person) WHEN type = 2 THEN (SELECT company) ELSE null END; (albeit this is not plpgsql anymore) Obviously you want to choose one of the approaches ;) NB. Prettige kerstdagen alvast ! -- Best, Frank.