The appropriate syntax for creating a temp table is
create temp table...
but you must definitely drop the temp table before the function exists. In addition, I would suggest this syntax instead:
CREATE TEMP TABLE IF NOT EXISTS temp_table AS SELECT id, value FROM test.another_table;
So your function will look like this:
CREATE FUNCTION test.myfunction() RETURNS SETOF test.out_table AS $$ CREATE TEMP TABLE IF NOT EXISTS temp_table AS SELECT id, value FROM test.another_table; INSERT INTO test.out_table SELECT id, value FROM temp_table; DROP TABLE temp_table; $$ LANGUAGE SQL;
But if I can be so kind, I would like to rewrite this function so that it is more correct:
CREATE FUNCTION test.myfunction() RETURNS TABLE (id int, value varchar)
untested; let me know if this fails.
source share