sql procedure creates temporary table which should used in other sql procedure. I tried
CREATE or replace FUNCTION f_createquery()
RETURNS TABLE ( kuupaev date
) AS $f_createquery$
-- actually this is big and time consuming select statement which should evaluated only once:
select current_date as kuupaev
$f_createquery$ LANGUAGE sql STABLE;
CREATE or replace FUNCTION f_usequery()
RETURNS TABLE ( kuupaev date
) AS $f_usequery$
-- big query tehing is used several times in query:
select kuupaev from tehing
union all
select kuupaev+1 from tehing
union all
select kuupaev+2 from tehing
$f_usequery$ LANGUAGE sql STABLE;
with tehing as (
select * from f_createquery() _
)
select * from f_usequery() _
but got error
ERROR: relation "tehing" does not exist
tehing contains temporary data created by stored procedure and it does not exist in database. How to allow other stored procedure to use it ? How to fix it for Postgres 9.1+ ?
Is there simething like
external table (kuupaev date)
which allows to define external tables? In real applicaton select statement in f_createquery is big and time consuming and should evaluated only once.
Replacing select * from tehing
to dynamic sql in f_usequery()
probably works but this prevents procedure compiling at run time. Is there better solution or can tehing passed in better way to other stored procedure, e.q. like parameter ?
Or should f_createquery create temporary table with fixed name tehing?