Select multiple rows as array Select multiple rows as array arrays arrays

Select multiple rows as array


In declare

  DECLARE id_nums bigint[];

in select

id_nums :=  ARRAY(select cust_id from Customers WHERE name = CName);

If you prefer loop use

DECLARE id_num bigint;FOR id_num in select cust_id from Customers WHERE name = CName LOOP  your code hereEND LOOP;

Read plpgsql control structures in postgresql docs 9.1.


To put data from individual rows into an array, use an array constructor:

DECLARE id_nums int[];  -- assuming cust_id is of type intid_nums := ARRAY (SELECT cust_id FROM customers WHERE name = cname);

Or the aggregate function array_agg()

id_nums := (SELECT array_agg(cust_id) FROM customers WHERE name = cname);

Or use SELECT INTO for the assignment::

SELECT INTO id_nums       ARRAY (SELECT cust_id FROM customers WHERE name = cname);