Can any one please help me, how to assign result of select query to a array variable since result can be an array of values.
1 Answer
There are a couple of different approaches you could take to get data into your array. The first would be a simple loop, as in the following:
DECLARE
TYPE NUMBER_ARRAY IS VARRAY(100) OF NUMBER;
arrNums NUMBER_ARRAY;
i NUMBER := 1;
BEGIN
arrNums := NUMBER_ARRAY();
FOR aRow IN (SELECT NUMBER_FIELD
FROM A_TABLE
WHERE ROWNUM <= 100)
LOOP
arrNums.EXTEND;
arrNums(i) := aRow.SEQUENCE_NO;
i := i + 1;
END LOOP;
end;
Another, as suggested by @Rene, would be to use BULK COLLECT, as follows:
DECLARE
TYPE NUMBER_ARRAY IS VARRAY(100) OF NUMBER;
arrNums NUMBER_ARRAY;
BEGIN
arrNums := NUMBER_ARRAY();
arrNums.EXTEND(100);
SELECT NUMBER_FIELD
BULK COLLECT INTO arrNums
FROM A_TABLE
WHERE ROWNUM <= 100;
end;
Share and enjoy.