Skip to Main Content
  • Questions
  • How to change the subscript/index value in an associative array?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mano.

Asked: May 21, 2018 - 6:05 pm UTC

Last updated: May 22, 2018 - 7:25 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Tom,


Is it possible to change the subscript/index value of an existing element in an associative array?

 declare
  type a_arr is table of varchar2(20) index by pls_integer;
  tb1 a_arr;
 begin
  tb1(1) := 'aaaa';
  tb1(2) := 'bbbb';
  tb1(3) := 'cccc';
 end;
 /


In the above associative array tb1, is it possible to change the subscript value from 1 to 10 (ie from tb1(1) to tb1(10)) without deleting or creating a new element in the table?

and Connor said...

Sorry - but no. You'd need to do something like

SQL>   declare
  2     type a_arr is table of varchar2(20) index by pls_integer;
  3     tb1 a_arr;
  4    begin
  5     tb1(1) := 'aaaa';
  6     tb1(2) := 'bbbb';
  7     tb1(3) := 'cccc';
  8
  9     tb1(10) := tb1(1);
 10     tb1(20) := tb1(2);
 11     tb1(30) := tb1(3);
 12     tb1.delete(1);
 13     tb1.delete(2);
 14     tb1.delete(1,3);
 15
 16    end;
 17   /

PL/SQL procedure successfully completed.


and obviously that gets more complicated if the target indices overlap the source ones, in which case I'd probably copy them to a new variable.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Solves the doubt

Mano, May 22, 2018 - 9:21 am UTC

Thanks a lot for the clear cut answer.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library