title: SELECT .. INTO to create a table in PL/pgSQL
body: <p>I want to use <code>SELECT INTO</code> to make a temporary table in one of my functions. <code>SELECT INTO</code> works in SQL but not PL/pgSQL.</p>
  
  <p>This statement creates a table called mytable (If <code>orig_table</code> exists as a relation):</p>
  
  <pre><code>SELECT *
  INTO TEMP TABLE mytable
  FROM orig_table;
  </code></pre>
  
  <p>But put this function into PostgreSQL, and you get the error: <code>ERROR:  "temp" is not a known variable</code></p>
  
  <pre><code>CREATE OR REPLACE FUNCTION whatever()
  RETURNS void AS $$
  BEGIN
      SELECT *
      INTO TEMP TABLE mytable
      FROM orig_table;
  END; $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>I can <code>SELECT INTO</code> a variable of type <code>record</code> within PL/pgSQL, but then I have to define the structure when getting data out of that record. <code>SELECT INTO</code> is really simple - automatically creating a table of the same structure of the <code>SELECT</code> query. Does anyone have any explanation for why this doesn't work inside a function?</p>
  
  <p>It seems like <code>SELECT INTO</code> works differently in PL/pgSQL, because you can select into the variables you've declared. I don't want to declare my temporary table structure, though. I wish it would just create the structure automatically like it does in SQL.</p>