| From: | "Peter Alberer" <h9351252(at)obelix(dot)wu-wien(dot)ac(dot)at> | 
|---|---|
| To: | "'sferriol'" <sylvain(dot)ferriol(at)imag(dot)fr>, <pgsql-general(at)postgresql(dot)org> | 
| Subject: | Re: dynamic views | 
| Date: | 2004-02-25 09:54:56 | 
| Message-ID: | 000201c3fb85$71283b30$5be0d089@ekelhardt | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
Hi sylvain,
i think what you need is a so-called "set-returning-function":
if you just need sql (example with a table called user_data):
create or replace function user_info(integer)
returns setof user_data as '
    select * from user_data where user_id = $1;
' language 'sql';
if you need plpgsql:
create or replace function user_info(integer)
returns setof user_data as '
declare
	p_user_id alias for $1;
	v_row record;
begin
	for v_row in select * from user_data where user_id = p_user_id
	loop
		-- business logic here, eg. Some if-statements or
sub-queries
		-- write a row to the result set
		return next v_row;
	end loop;
return;
' language 'plpgsql';
if you want to return rows that do not come from one single table you
will probably need to create a type:
create type user_data as (
    user_id integer,
    username varchar
);
you can then use that type in the "returns setof TYPE" clause of the
function.
Hope that helps. You should search for info about set-returning
functions for more details.
>-----Ursprüngliche Nachricht-----
>Von: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-
>owner(at)postgresql(dot)org] Im Auftrag von sferriol
>Gesendet: Dienstag, 24. Februar 2004 17:30
>An: pgsql-general(at)postgresql(dot)org
>Betreff: [GENERAL] dynamic views
>
>hello
>is it possible with postgres 7.2 or more, to define a dynamic view.
>For example, i have a table with a column 'user'
>and i want to define a view which gives infomrations from different
>tables but the user  has to specifie the 'user' parameter when using a
>select to the view
>
>sylvain
>
>
>---------------------------(end of
broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to
majordomo(at)postgresql(dot)org)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Peter Alberer | 2004-02-25 09:56:56 | Re: dynamic views | 
| Previous Message | Shridhar Daithankar | 2004-02-25 08:58:31 | Re: Why does app fail? |