title: Postgres Stored Procedure issue - how do I solve this?
body: <p>I am creating a stored procedure that looks like this:</p>
  
  <pre><code>CREATE FUNCTION get_components(_given_user_id integer) RETURNS TABLE (id integer, name    varchar, active boolean, parent integer, type smallint, description varchar, email varchar, user_id integer, component_id integer, flag smallint) AS $body$
  BEGIN
  RETURN QUERY SELECT s.* FROM st_components s LEFT JOIN (SELECT a.* FROM st_users_components_perms a WHERE a.user_id=_given_user_id) ON a.component_id=s.id ORDER BY name;
  END;
  $body$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>The problem is, ON a.component_id=s.id ORDER BY name doesn't work because a.component_id is out of scope at this point. Is there a way to declare "a" as st_users_components_perms outside of the query? How is this solved? Thank you very much for any insight!</p>