I am trying to create a function which will dynamically create a unique table name, populate the table with a result set returned from another function, and return the table name to the caller. In outline, it looks something like this:
CREATE OR REPLACE FUNCTION return_result_table(
_param1 integer[],
_param2 text[])
RETURNS text
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
_table_name TEXT;
_select TEXT;
BEGIN
_table_name := '_XYZABC_' || replace(current_date::text,'-','_') || '_' || 'ZXCVBN'; -- not the real code
_select := '(select * from some_other_function(_param1, _param2))';
execute 'create table some_schema.' || _table_name || ' as ' || _select;
return _table_name;
END;
$BODY$;
The code is accepted, and it appears to be forming the command correctly as
execute 'create table some_schema._XYZABC_2020_10_07_ZXCVBN as (select * from some_other_function(_param1, _param2))'
but when I try to execute it with:
SELECT return_result_table(ARRAY[0,1,5,19],ARRAY['AA,'BB','CC'])
I get the error
ERROR: column "_param1" does not exist
LINE 1: ...ect * from return_result_table(_param1...
It appears that the code is trying to substitute in the value of a column (of what?) rather than using the parameter.
How can I prevent this translation happening, so that I can pass the parameters through into the called function?