MY 2ND ATTEMPTI tried the following:
In response to @Erwin Brandstetter I've fixed my code to become the following:
Note, that my function outputs now TEXT, and not TABLE (as indeed, as you pointed out, there is no point in outputting a TABLE object, since we would need to define its all its columns ahead, which basically defies the entire purpose):
CREATE OR REPLACE FUNCTION myresultmytext (mytable text, myprefix text)
RETURNS textSETOF RECORD AS $$
declare
$func$
DECLARE
smalltext text;
myoneliner text;
BEGIN
SELECT BEGIN
'SELECT ' || substr(cols,2,length(cols)-2) ||' FROM '||mytable SELECT
INTO myoneliner
FROM (
'SELECTSELECT 'array(
SELECT ||DISTINCT string_agg(quote_ident(column_name::text), ',' ORDER BY column_name)
|| ' FROM ' || quote_ident(mytable)information_schema.columns
FROM information_schema.columns
WHERE table_name = mytable
AND column_name LIKE myprefix||'%'
AND table_schema = 'public'; --order schemaby name;quote_ident might be another param
RAISE NOTICE 'My additional text)::text %',cols myoneliner;
RETURN) myoneliner;sub;
END
$func$ LANGUAGE plpgsql;smalltext=lower(myoneliner);
raise notice '%','my --nowadditional calltext function'||smalltext;
select myresult('dkj_p_k27ac','enri');
--nowRETURN callQUERY theEXECUTE functionsmalltext;
END;
$$ LANGUAGE plpgsql;
Call function:
SELECT * selectfrom myresultmytext('dkj_p_k27ac','enri');
And now, upon running the above procedure - IBut I'm getting the following error message, could you please advise what should I change in order for it to execute?:
ERROR: get a textcolumn string,definition whichlist is basically a query (I'll refer to it up next as 'oneliner-output', justrequired for simplicity). As follows (i just copy/paste it from the one output cell that i've gotfunctions intoreturning here):
"record"
"SELECTLINE enrich_d_dkj_p_k27ac,enrich_lr_dkj_p_k27ac,enrich_r_dkj_p_k27ac26: FROMSELECT dkj_p_k27ac"
* please note that the double quotes from both sides of the statement were part of the myresult() output mytext(i didn't add them by myself'dkj_p_k27ac','enri').;
@Erwin Brandstetter, and @Craig Ringer, I understand now much better the problematic idea of thinking to construct a single function that would both create the 'oneliner-output' AND execute it.~Thanks a lot for all the informative and helpful input you********** gave!!Error **********
But - basically, I am able to copy/paste the 'oneliner-output' into a new postgres query window and execute it as a normal query just fine - receiving the desired columns and rows in my Data Output window. I would like however to automate this step, so to avoid the copy/paste step. Is there a way in postgres to use the TEXT output (the 'oneliner-output') that I receive from myresult() function, and execute it? Can
ERROR: a second function be created that would receive the output ofcolumn myresult()definition andlist useis itrequired for executing afunctions query?returning "record"
Thanks a lot!SQL state: 42601
Character: 728