title: How to execute a string result of a stored procedure in postgres
body: <p>I have created the following stored procedure, which basically receives a name of table, and a prefix. The function then finds all columns that share this prefix and returns as an output a 'select' query command ('myoneliner'). 
  as follows:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION mytext (mytable text, myprefix text)
  RETURNS text AS $myoneliner$
  declare
      myoneliner text;
  BEGIN
     SELECT 'SELECT ' || substr(cols,2,length(cols)-2) ||' FROM '||mytable 
     INTO myoneliner  
       FROM (
          SELECT array(
             SELECT DISTINCT quote_ident(column_name::text)
             FROM   information_schema.columns
             WHERE  table_name = mytable
             AND    column_name LIKE myprefix||'%'
             order by quote_ident             
        )::text cols 
       ) sub;
     RETURN myoneliner;
  END;
  $myoneliner$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>Call:</p>
  
  <pre><code>select mytext('dkj_p_k27ac','enri');
  </code></pre>
  
  <p>As a result of running this stored procedure and the 'select' that is following it, I get the following output at the Data Output window (all within one cell, named "mytext text"):</p>
  
  <pre><code>'SELECT enrich_d_dkj_p_k27ac,enrich_lr_dkj_p_k27ac,enrich_r_dkj_p_k27ac
   FROM dkj_p_k27ac'
  </code></pre>
  
  <p>I would like to basically be able to take the output command line that I received as an output and execute it. In other words, I would like to be able and execute the output of my stored procedure. 
  How can I do so? </p>
  
  <p>I tried the following:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION mytext (mytable text, myprefix text)
  RETURNS SETOF RECORD AS $$
  declare
          smalltext text;
      myoneliner text;
  BEGIN
     SELECT 'SELECT ' || substr(cols,2,length(cols)-2) ||' FROM '||mytable 
     INTO myoneliner  
       FROM (
          SELECT array(
             SELECT DISTINCT quote_ident(column_name::text)
             FROM   information_schema.columns
             WHERE  table_name = mytable
             AND    column_name LIKE myprefix||'%'
             order by quote_ident             
        )::text cols 
       ) sub;
  
     smalltext=lower(myoneliner);
     raise notice '%','my additional text '||smalltext;
     RETURN QUERY EXECUTE smalltext;
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>Call function:</p>
  
  <pre><code>SELECT * from mytext('dkj_p_k27ac','enri');
  </code></pre>
  
  <p>But I'm getting the following error message, could you please advise what should I change in order for it to execute?:</p>
  
  <pre><code>ERROR:  a column definition list is required for functions returning "record"
  LINE 26: SELECT * from mytext('dkj_p_k27ac','enri');
  
  ********** Error **********
  
  ERROR: a column definition list is required for functions returning "record"
  SQL state: 42601
  Character: 728
  </code></pre>