title: PL/pgSQL functions - loop through specific column and execute second query in loop
body: <p>As you can see in title, I want to loop through specific column which holds building IDs and then function will execute a second query that uses these looped values and show query result in PostgreSQL. </p>
  
  <p>Please note that the code below:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION getBID() RETURNS SETOF building AS
  $BODY$
  DECLARE
      r building%ROWTYPE;
  BEGIN
      FOR r IN EXECUTE 'SELECT point_id FROM building'
  
      LOOP
  
      RETURN QUERY EXECUTE 'SELECT gid, nof, year 
      FROM building 
      WHERE point_id = ' || r;
  
      END LOOP;
      RETURN;
  END
  $BODY$
  LANGUAGE 'plpgsql' ;
  
  SELECT * FROM getBID();
  </code></pre>
  
  <p>My building IDs are integer values. I wonder two aspects as follows:</p>
  
  <ul>
  <li>"r" variable should be declared as "integer"?</li>
  <li>my second query should be used in loop, too?</li>
  </ul>
  
  <p>Thanks in advance...</p>