pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Wilkinson, Jim <Jim.Wilkinson@cra-arc.gc.ca>
Cc: pgsql-sql@postgresql.org
Subject: Re: Function to return a multiple colmn table or view
Date: Fri, 20 Apr 2007 15:32:28 +0100
Message-ID: <4628CEFC.7020801@archonet.com> (raw)
In-Reply-To: <7CCC5BEF5E72394C963E529B54EB4A642A19F1@SD01ITMV12.PROD.NET>
References: <7CCC5BEF5E72394C963E529B54EB4A642A19F1@SD01ITMV12.PROD.NET>

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



view thread (3+ messages)  latest in thread

Message-ID: <4628CEFC.7020801@archonet.com>
Permalink:  ../4628CEFC.7020801@archonet.com/
Also on:    postgresql.org/message-id/4628CEFC.7020801@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, Jim.Wilkinson@cra-arc.gc.ca
  Subject: Re: Function to return a multiple colmn table or view
  In-Reply-To: <4628CEFC.7020801@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox