Обсуждение: Returning data from multiple functions

Поиск
Список
Период
Сортировка

Returning data from multiple functions

От
tlund79
Дата:
A short brief about the goal:
I have a Excel workbook loaded with static data and need to compare these to
"live-data" from DB. There are 20 rows with data in the report that needs to
be retrieved from the database, and almost the same amount of queries, since
all of these queries relies on variable input I've created on function per
cell. This solution works both in terms of maintenance and less "query work
in Excel". However, all of these functions do have the same variable input,
thus I was hoping to create one function that runs all of the functions and
input the variables. This enables me to make the excel workbook maintenance
free and all of the logic is put in one place.

This is were I'm stuck, creating and selecting one and one function works
fine, but creating one function to run and retrieve data from multiple
functions seems like a challenge. What I basically want is a 'union all',
but when I try to run a union all on the functions it only retrieves data
from the last function.

This is what I got now (i've inputed the variables manually in each function
for testing purposes):

CREATE OR REPLACE FUNCTION ppr_test_to_funk() RETURNS SETOF RECORD AS
$BODY$

DECLARE 

ppr RECORD;

BEGIN
select ppr_pf_inn_verdi(2011,1,52,array[3], array[7,4])
union all
select ppr_pf_inn_antall(2011,1,52,array[3], array[7,4]);

RETURN NEXT ppr;
END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000;
ALTER FUNCTION ppr_test_to_funk() OWNER TO oystein;

When I select this one I get the usual "Query has no destination for result
data". If my suspicions are correct I'm struggelig with the "RETURNS SETOF"
combined with the destination.


And yes; I'm totally new to all of this.

Thanks in advance


--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Returning-data-from-multiple-functions-tp4980747p4980747.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.


Re: Returning data from multiple functions

От
tlund79
Дата:
I know got this far thanks to Pavle Stehule. The function worked and returned
the data when the variables was predefined after "return query".

When tried to replace these with variables passed through the function call
I got this message;
ERROR:  syntax error at or near "RETURN"
LINE 1: ...ll select ppr_pf_inn_antall( $1 , $2 , $3 , $4 , $5 ) RETURN
              ^
 
QUERY:   select ppr_pf_inn_verdi( $1 , $2 , $3 , $4 , $5 ) union all select
ppr_pf_inn_antall( $1 , $2 , $3 , $4 , $5 ) RETURN
CONTEXT:  SQL statement in PL/PgSQL function "ppr_test_to_funk" near line 7



CREATE OR REPLACE FUNCTION ppr_test_to_funk(aarstall int, frauke int, tiluke
int, prosjektkode int[], teamkode int[]) RETURNS setof integer AS
$BODY$

I called the function with this: select * from
ppr_test_to_funk(2011,1,52,array[3], array[7,4])

Am I lost or are this possible?

*Updated function:*

BEGIN
return query
select ppr_pf_inn_verdi($1,$2,$3,$4,$5)
union all
select ppr_pf_inn_antall($1,$2,$3,$4,$5)

RETURN;
END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION ppr_test_to_funk() OWNER TO oystein;

--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Returning-data-from-multiple-functions-tp4980747p4980786.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.


Re: Returning data from multiple functions

От
Pavel Stehule
Дата:
2011/11/10 tlund79 <thomas.lund@eniro.no>:
> I know got this far thanks to Pavle Stehule. The function worked and returned
> the data when the variables was predefined after "return query".
>
> When tried to replace these with variables passed through the function call
> I got this message;
> ERROR:  syntax error at or near "RETURN"
> LINE 1: ...ll select ppr_pf_inn_antall( $1 , $2 , $3 , $4 , $5 ) RETURN
>                                                                 ^
> QUERY:   select ppr_pf_inn_verdi( $1 , $2 , $3 , $4 , $5 ) union all select
> ppr_pf_inn_antall( $1 , $2 , $3 , $4 , $5 ) RETURN
> CONTEXT:  SQL statement in PL/PgSQL function "ppr_test_to_funk" near line 7
>
>
>
> CREATE OR REPLACE FUNCTION ppr_test_to_funk(aarstall int, frauke int, tiluke
> int, prosjektkode int[], teamkode int[])
>  RETURNS setof integer AS
> $BODY$
>
> I called the function with this: select * from
> ppr_test_to_funk(2011,1,52,array[3], array[7,4])
>
> Am I lost or are this possible?
>
> *Updated function:*
>
> BEGIN
> return query
> select ppr_pf_inn_verdi($1,$2,$3,$4,$5)
> union all
> select ppr_pf_inn_antall($1,$2,$3,$4,$5)

>>> MISSING SEMICOLON HERE!!!

>
> RETURN;
> END;
> $BODY$
>  LANGUAGE plpgsql VOLATILE
>  COST 100;
> ALTER FUNCTION ppr_test_to_funk()
>  OWNER TO oystein;
>
> --
> View this message in context:
http://postgresql.1045698.n5.nabble.com/Returning-data-from-multiple-functions-tp4980747p4980786.html
> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>