I am trying to make the following function working:
CREATE OR REPLACE FUNCTION validate_count(devices TEXT[], campaign_id INTEGER) RETURNS void AS $$ DECLARE devices_array TEXT[] := devices; devices_count INTEGER := array_length(devices, 1); row_id INTEGER := campaign_id; BEGIN FOR device IN unnest(devices_array) LOOP IF my_count('my_table', device, row_id) != 1; RAISE EXCEPTION 'invalid_count %', row_id ENDIF END LOOP; END $$ LANGUAGE plpgsql; my_count is a working function which returns INTEGER.
The definition fails with the error:
ERROR: syntax error at or near "unnest" LINE 7: FOR device IN unnest(devices_array) LOOP Could you spot the issue? Thanks!
I am planning to call the function as follows:
select validate_count('{foo, bar}', 1)