Skip to main content
← PL/SQL Topics

Collections

Explain associative arrays indexed by VARCHAR2

Associative Arrays Indexed by VARCHAR2

ExplanationOracle Fusion / EBS · Technical · PL/SQLHigh confidenceGenerated

Using a string key turns an associative array into a hash-map / dictionary keyed by name.

How it works

TABLE OF value_type INDEX BY VARCHAR2(n) lets you subscript by text: c('EUR') := 1.09. Keys are stored in sorted order, so FIRST/NEXT iterate alphabetically. A missing key read raises NO_DATA_FOUND — guard with IF c.EXISTS(key). This is the go-to structure for in-memory caches (currency rates, lookup translations, de-dup sets) built once and probed many times inside a loop, avoiding repeated SQL.

How it works
DECLARE
  TYPE rate_map IS TABLE OF NUMBER INDEX BY VARCHAR2(3);
  rates rate_map;
BEGIN
  FOR r IN (SELECT currency_code, rate FROM daily_rates WHERE rate_date = TRUNC(SYSDATE)) LOOP
    rates(r.currency_code) := r.rate;
  END LOOP;

  IF rates.EXISTS('GBP') THEN
    DBMS_OUTPUT.PUT_LINE('GBP rate = ' || rates('GBP'));
  END IF;
END;
/

Related questions

GroundingGenerated

Model-generated, grounded against the curated knowledge layer. Check specifics against your instance.

Have a follow-up, or a different question?

Continue in Ask Oracle AI