Jagger
Jagger

Reputation: 10524

How to easily refer to the index type of an associative array?

Is there an easy way to refer to the index type of an associative array in PLSQL while declaring a variable that represents its key?

I am looking for a language construct similar to the following one.

DECLARE
  i number;
  j i%type;
BEGIN
  null;
END;

I would like to be able to do something like that.

DECLARE
  type ty_my_type is table of number index by varchar2(4);
  my_array ty_my_type;
  -- key my_array.key%type;
  -- or
  -- key my_array%keytype;
BEGIN
  null;
END;

Upvotes: 2

Views: 56

Answers (1)

MT0
MT0

Reputation: 167981

You can declare a SUBTYPE and use that:

DECLARE
  SUBTYPE KEY_TYPE IS VARCHAR2(4);
  TYPE ASSOC_ARRAY_TYPE IS TABLE OF NUMBER INDEX BY KEY_TYPE;

  my_array ASSOC_ARRAY_TYPE;
  my_key   KEY_TYPE;
BEGIN
  NULL;
END;
/

Upvotes: 5

Related Questions