I have a function that takes 3 parameters: huc, id_list, and email.
CREATE OR REPLACE FUNCTION my_app.job_batch(
huc text,
input_list text[],
email text
) RETURNS VOID AS
$$
DECLARE
id text;
BEGIN
FOREACH id IN ARRAY input_list LOOP
EXECUTE 'SELECT * FROM my_app.my_funct(
' || huc || '::text,
' || id || '::text,
' || email || '::text)';
END LOOP;
END;
$$
LANGUAGE plpgsql;
When I try to run the function however, it throws an error: ERROR: column "myhu4" does not exist
SELECT * FROM spg_app.append_spg_job_batch('MYHUC4', array['1021', '1025','1026','1027','0701','0702','0703','0708','0709'], '[email protected]');
Why is it referring to myhuc4 as a column and why is displaying it in lower case. Is my syntax below to run the function with those 3 parameters incorrect? Note: If I run the below hardcoded version, it runs fine:
DO $$
DECLARE
id_list text[] := array['1021', '1025','1026','1027','0701','0702','0703','0708','0709'];
id text;
BEGIN
FOREACH id in ARRAY id_list LOOP
EXECUTE 'SELECT * FROM my_app.my_funct(
''MYHU4''::text,
' || id || '::text,
''[email protected]''::text)'
END LOOP;
END;
$$
LANGUAGE plpgsql;
textdata type for params, in "manual" -varchar. This types are not the same. Second - in "manual" you enclose the textual values in apostrophes, but in "dynamic" - do not. And I think the second is the cause of error - without apostrophes' || huc || '::textwill becomeMYHUC4::text, that's can be treated as a column name with conversion totextdata type. The same issue with' || email || '::texttextcasting when I plug in the parameters?my_funct(). 2)column "myhu4" does not existis happening because the value forhucis being double quoted at some point e.g. "myhu4" and that is being taken as a column identifier. This why we need to 1) to be done.EXECUTEin the first place? You do not want to construct a dynamic query here. JustSELECT * FROM my_app.my_funct(huc, id, email);!