Temporary table postgresql function
<p>I can't find a clear explanation of the syntax to create (and use) tables just for the inside calculations of a function. Could anyone give me a syntax exemple please ?</p>

<p>From what I've found, I have tried this (with and without <code>@</code> before <code>temp_table</code>) :</p>

<pre><code>CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$

DECLARE @temp_table TABLE
( 
        id int,
        value text
 )
BEGIN
 INSERT INTO @temp_table 
        SELECT id, value
        FROM test.another_table;

 INSERT INTO test.out_table
        SELECT id, value
        FROM @temp_table;
RETURN END
$$ LANGUAGE SQL;
</code></pre>

<p>I get :</p>

<blockquote>
  <p>ERROR:  syntax error at or near "DECLARE"
     LINE 5:  DECLARE @temp_table TABLE</p>
</blockquote>

<p>-</p>

<p>I also tried the CREATE TABLE approach suggested <a href="https://stackoverflow.com/a/23654146/4348534">here</a>, this way : </p>

<pre><code>CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$

    CREATE TABLE temp_table AS
        SELECT id, value
        FROM test.another_table;

    INSERT INTO test.out_table
        SELECT id, value
        FROM temp_table;

$$ LANGUAGE SQL;
</code></pre>

<p>And I get this :</p>

<blockquote>
  <p>ERROR:  relation "temp_table " does not exist
       LINE 11:   FROM temp_table </p>
</blockquote>

<p>(Obviously, I'm aware the temp_table is not necessary for what I'm doing in the code above, but that's not the point :) => I want to understand the syntax to get it to work)</p>