title: Return a select from a plpgsql function
body: <p>I need to return a dynamically generated select statement from a plpgsql function. This is what I have so far:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION qa_scf(cname character varying, tname character varying)
  RETURNS text AS
  $BODY$
  BEGIN
  return '* from ' ||tname|| 'where ' ||cname ||' != ''AL''';
  end;
  $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  </code></pre>
  
  <p>The caller, ran from a batch file:</p>
  
  <pre><code>select qa_scf('state', 'testtable')
  </code></pre>
  
  <p>This returns the literal text "qa_scf * from testtable where state != 'AL'". I need to run this query from an sql batch file, but I cannot seem to find the right return statement to have this function return a string and then have the sql batch execute it. I'm using Postgres 9.0. </p>