14

i have some numbers which i want to store in an array. how will i declare array and assign value to it in oracle pl/sql??

2
  • Do you have different sets of those numbers or only one ? Commented Aug 23, 2010 at 7:28
  • i have number like 1,2,5,10,100 i want to store it in an array like array[0] = 1, array[1] = 2, ..etc afterall i want to acces it like array[i] Commented Aug 23, 2010 at 7:29

1 Answer 1

24

There are array type in PL/SQL but we can create those ourselves using the table

declare 
  type NumberArray is table of number index by binary_integer;
  myArray NumberArray;
begin

   myArray(0) := 1
   myArray(1) := 2 
   --or use a for loop to fill
end;

The explanation article

EDIT :

or as Adam Musch said if we know the data size of data, that we are operating on, we can use VARRAYs that are length fixed, this is oracle environment, so subscripts start from 1,

Alternative is using VARRAY, where array subscript starts from 1 and the length of VARRAYs is fixed.

Semantic:

declare  type VarrayType is varray(size) of ElementType;

Example :

    declare
      type NumberVarray is varray(100) of NUMERIC(10);
      myArray NumberVarray;
    BEGIN
      myArray := NumberVarray(1,10,100,1000,10000);

      myArray(1) = 2;

      for i in myArray.first..myArray.last
      loop
        dbms_output.put_line('myArray(' || i || '): ' || myArray(i));
      end loop;  
    end;
END;

Output :

myArray(1) : 2
myArray(2) : 10
myArray(3) : 100
myArray(4) : 1000
myArray(5) : 10000
Sign up to request clarification or add additional context in comments.

1 Comment

There is an ARRAY type in PL/SQL; it's called a VARRAY, which is a 1-indexed array with a fixed upper bound of elements. What you're using is an associative array, which is more like a Java HashMap, where the BINARY_INTEGER is the key and the NUMBER is the value.

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.