I am using a temporary table in a function to save some results however I don't know how to return the table from the function. Ideally I would like to do everything in one query (i.e. not two queries: one for calling the function, the other to get data from the temp table).
Currently my main_function()
is as follows:
CREATE OR REPLACE FUNCTION main_function() RETURNS void AS
$BODY$
BEGIN
DROP TABLE IF EXISTS temp_t CASCADE;
CREATE TEMP TABLE temp_t AS SELECT * FROM tbl_t limit 0;
EXECUTE 'INSERT INTO temp_t ' || 'SELECT * FROM tbl_t limit 10';
END;
$BODY$
LANGUAGE 'plpgsql' ;
And I am calling it like so:
SELECT * from main_function();
SELECT * from temp_t;
Again, the problem is that I don't actually want to call the second query. The first query should return the temp table as a result, however I cannot do this since the temp table is created in main_function()
so it cannot be its return type.
Any ideas on how to achieve this?
Thanks