1

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
  • The term you are looking for is: "Oracle bulk collect". Search the internet and you will find lots of examples. Commented May 29, 2012 at 10:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.