Create an Index By Table of Record

An index by table is an associative array.
An associative array never has more than two columns: the variable being indexes and the index value.

However you can combine a record with an associative array to tie multiple values to the same index.

 

In this case the array still has a variable and an index, but the record variable now contains multiple distinct values.
In the below example you’ll see how to associate a record to an associative array.

 

DECLARE
  TYPE r_impiegati IS RECORD (
                               imp_id          NUMBER      ,
                               imp_nome        VARCHAR2(30),
                               imp_cognome     VARCHAR2(30)
                             );
  r_imp_rec   r_impiegati;
  TYPE t_imp IS TABLE OF r_impiegati
  INDEX BY PLS_INTEGER;
  t_imp_tab t_imp;
BEGIN
  r_imp_rec.imp_id      := 1;
  r_imp_rec.imp_nome    := ‘Domenico’;
  r_imp_rec.imp_cognome := ‘Berardi’;
  t_imp_tab(1)          := r_imp_rec;
  r_imp_rec.imp_id      := 2;
  r_imp_rec.imp_nome    := ‘James’;
  r_imp_rec.imp_cognome := ‘Bond’;
  t_imp_tab(2)          := r_imp_rec;
  DBMS_OUTPUT.PUT_LINE(‘Impiegato numero ‘||t_imp_tab(1).imp_id);
  DBMS_OUTPUT.PUT_LINE(t_imp_tab(1).imp_nome);
  DBMS_OUTPUT.PUT_LINE(t_imp_tab(1).imp_cognome);
  DBMS_OUTPUT.NEW_LINE;
  DBMS_OUTPUT.PUT_LINE(‘———-‘);
  DBMS_OUTPUT.NEW_LINE;
  DBMS_OUTPUT.PUT_LINE(‘Impiegato numero ‘||t_imp_tab(2).imp_id);
  DBMS_OUTPUT.PUT_LINE(t_imp_tab(2).imp_nome);
  DBMS_OUTPUT.PUT_LINE(t_imp_tab(2).imp_cognome);
END;
The output will be:
 Impiegato numero 1
 Domenico
 Berardi
 ———-
 Impiegato numero 2
 James
 Bond

Potrebbero interessarti anche...