title: How do I return results from multiple queries (that have been looped) in one table?
body: <p>In plpgsql, I'd like to use results from query A to execute several queries (queries B), and return the results of all B queries as one table, but I'm not sure how to do this. Do I use the 'RETURN QUERY' functionality in plpgsql?</p>
  
  <p>Here's an example. The function finds all zip_code records with the given code, and then searches all locations for a state of that original zip_code. (I know the current query could be done via a join, but my ACTUAL query would require this functionality.)</p>
  
  <pre><code>begin;
  create OR REPLACE function t() returns setof locations as
  $$
  declare z zip_codes%rowtype;
  begin
    for z in select * from zip_codes where code like '%32301%'
    LOOP
      return query select * from locations where locations.state like z.state; #query B 
          # All I want to do is return the results from all of the above queries as one
          # result set.
    END LOOP;
    return;
  end
  $$
  language 'plpgsql';
  commit;
  </code></pre>
  
  <p>Any idea how I do that?</p>