I'm trying to loop through few fields and run a function on them:
FOR field IN ARRAY['f1','f2'] LOOP
execute pg_temp.converFieldToLower(newTableNameRaw,field)
END LOOP;
This is the function i'm trying to use:
CREATE OR REPLACE FUNCTION pg_temp.converFieldToLower(t varchar, f varchar) RETURNS void AS $$
#variable_conflict use_variable
BEGIN
EXECUTE concat_ws (' ', 'UPDATE',t,'SET',f,'= LOWER(',f,')');
END;
$$ LANGUAGE plpgsql;
It looks like it's not the right way to declare an array, what am I doing wrong?
ERROR: syntax error at or near "ARRAY" LINE 49: FOR field IN ARRAY['f1','f2'] LOOP
The FOREACH
loop is designed specifically for iterating through the elements of an array value, e.g.:
FOREACH field IN ARRAY ARRAY['f1','f2'] LOOP
execute pg_temp.converFieldToLower(newTableNameRaw,field) into res;
END LOOP;
The feature was introduced in Postgres 9.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With