Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Sunday 11 March 2018

PL/SQL Arrays

Declaration

In PL/SQL arrays are denoted as VARRAY, which can store a fixed sized sequential characters as per the defined type and range.
It is similar to an array which is a collection of elements of similar type.


Varrays in PL/SQL


syntax

VARRAY is declared as :-

CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) of <element_type>

Where,

varray_type_name is a valid attribute name,
n is the number of elements (maximum) in the varray,
element_type is the data type of the elements of the array.

for exmaple,

type values is VARRAY(10) of integer;


Exmaple


declare
type namearray is varray(5) of varchar2(20);
type marks is varray(5) of Integer;
names namearray;
grades marks;
total number;
i number;
begin
  names :=namearray('sarfraz','alam','mayank','trivedi','shobhit');
grades := marks(100,90,80,80,70);
total := names.count;
dbms_output.put_line('Total is '|| total);
for i in 1..total
  loop
 
    dbms_output.put_line('Student: ' || names(i) || '
      Marks: ' || grades(i));
    end loop;
end;

1 comment: