total nube in PL/PGSQL. I would like to define an array of strings and then used that in my SELECT… WHERE In statement but can’t seem to get it to work, help appreciated.
DO $$
DECLARE
testArray varchar[] := array['john','lisa'];
ids integer[];
BEGIN
ids = array(select id from tableA where name in (testArray));
-- this works
ids = array(select id from tableA where name in ('john','lisa'));
END $$;
2
Answers
Use
array_agg
to group the ids, and with the operatorany
you can use the array of names in the where clause, e.g:You can use any near of testarray.