Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 7AAD79FB9F1 for ; Fri, 20 Apr 2007 11:32:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 20179-04 for ; Fri, 20 Apr 2007 11:32:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 59E069F942C for ; Fri, 20 Apr 2007 11:32:31 -0300 (ADT) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 792D57522C; Fri, 20 Apr 2007 15:32:29 +0100 (BST) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 126B92740D7; Fri, 20 Apr 2007 15:32:29 +0100 (BST) Message-ID: <4628CEFC.7020801@archonet.com> Date: Fri, 20 Apr 2007 15:32:28 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.0 (X11/20070326) MIME-Version: 1.0 To: "Wilkinson, Jim" Cc: pgsql-sql@postgresql.org Subject: Re: Function to return a multiple colmn table or view References: <7CCC5BEF5E72394C963E529B54EB4A642A19F1@SD01ITMV12.PROD.NET> In-Reply-To: <7CCC5BEF5E72394C963E529B54EB4A642A19F1@SD01ITMV12.PROD.NET> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/186 X-Sequence-Number: 28186 Wilkinson, Jim wrote: > I am new to psql , so please be patient ! > > Can someone please provide a small quick example of a a function that > take 1 paramater and based on that parameter, returns a table or view ? > > Etc .... Note this is just a abstract of the functon, not a working > function call !!! > > Create function viewtest( start_month) > > If $1 = 'April' > Then > Select * from april_view; > Return ; CREATE TABLE month_data (md_year int4, md_month int4, md_data text); CREATE VIEW jan_view AS SELECT * FROM month_data WHERE md_month=1; CREATE FUNCTION viewtest(startmonth int) RETURNS SETOF month_data AS $$ DECLARE sql text; r record; BEGIN IF startmonth = 1 THEN sql := 'SELECT * FROM jan_view'; ELSE sql := 'SELECT * FROM month_data'; END IF; FOR r IN EXECUTE sql LOOP RETURN NEXT r; END LOOP; RETURN; END; $$ LANGUAGE plpgsql; HTH -- Richard Huxton Archonet Ltd