Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8527187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:29:06+00:00 2026-06-11T08:29:06+00:00

I have an associative array created by a type of rowtype of a table

  • 0

I have an associative array created by a type of rowtype of a table column.

To give an example, this is how it is(the table names are different, but the structure is the same):

This is the DDL of the table

CREATE TABLE employees
  (
     id     NUMBER,
     name   VARCHAR2(240),
     salary NUMBER
  ); 

Here’s what my procedure is doing:

DECLARE
    TYPE table_of_emp
      IS TABLE OF employees%ROWTYPE INDEX BY BINARY_INTEGER;
    emp TABLE_OF_EMP;
BEGIN
    IF emp IS NULL THEN
      dbms_output.Put_line('Null associative array');
    ELSE
      dbms_output.Put_line('Not null');
    END IF;
END; 

I assume this should result in “Null associative array” being printed. However, the if condition fails and the execution jumps to the else part.

Now if I put in a for loop to print the collection values

DECLARE
    TYPE table_of_emp
      IS TABLE OF employees%ROWTYPE INDEX BY BINARY_INTEGER;
    emp TABLE_OF_EMP;
BEGIN
    IF emp IS NULL THEN
      dbms_output.Put_line('Null associative array');
    ELSE
      dbms_output.Put_line('Not null');

      FOR i IN emp.first..emp.last LOOP
          dbms_output.Put_line('Emp name: '
                               || Emp(i).name);
      END LOOP;
    END IF;
END; 

then the program unit raises an exception, referencing the for loop line

ORA-06502: PL/SQL: Numeric or value error

which I presume is because of the null associative array. Is the error being raised because of null associative array?

So why is the first check failing then? What am I doing wrong?

The database server is Oracle 11g EE (version 11.2.0.3.0 64 bit)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:29:08+00:00Added an answer on June 11, 2026 at 8:29 am

    I assume this should result in “Null associative array” being printed. That assumption is wrong for associative arrays. They exist when declared, but are empty. It would be correct for other types of PL/SQL collections:

    Until you initialize it, a nested table or varray is atomically null;
    the collection itself is null, not its elements. To initialize a
    nested table or varray, you use a constructor, a system-defined
    function with the same name as the collection type. This function
    constructs collections from the elements passed to it.

    You must explicitly call a constructor for each varray and nested
    table variable. Associative arrays, the third kind of collection, do
    not use constructors. Constructor calls are allowed wherever function
    calls are allowed. Initializing and Referencing Collections

    Compare:

    SQL> declare
      2      type varchar2_100_aa is table of varchar2(100) index by binary_integer;
      3      test varchar2_100_aa;
      4  begin
      5      test(1) := 'Hello';
      6      dbms_output.put_line(test(1));
      7  end;
      8  /
    Hello
    
    PL/SQL procedure successfully completed.
    
    SQL> declare
      2      type varchar2_100_va is varray(100) of varchar2(100);
      3      test varchar2_100_va;
      4  begin
      5      test(1) := 'Hello';
      6      dbms_output.put_line(test(1));
      7  end;
      8  /
    declare
    *
    ERROR at line 1:
    ORA-06531: Reference to uninitialized collection
    ORA-06512: at line 5
    

    Variable array done correctly:

    SQL> declare
      2      type varchar2_100_va is varray(10) of varchar2(100);
      3      test varchar2_100_va;
      4  begin
      5      test := varchar2_100_va(); -- not needed on associative array
      6      test.extend; -- not needed on associative array
      7      test(1) := 'Hello';
      8      dbms_output.put_line(test(1));
      9  end;
     10  /
    Hello
    
    PL/SQL procedure successfully completed.
    

    Because the associative array is empty first and last are null, which is why your second example results in ORA-06502: PL/SQL: Numeric or value error:

    SQL> declare
      2      type varchar2_100_aa is table of varchar2(100) index by binary_integer;
      3      test varchar2_100_aa;
      4  begin
      5      dbms_output.put_line(test.count);
      6      dbms_output.put_line(coalesce(to_char(test.first), 'NULL'));
      7      dbms_output.put_line(coalesce(to_char(test.last), 'NULL'));
      8      test(1) := 'Hello';
      9      dbms_output.new_line;
     10      dbms_output.put_line(test.count);
     11      dbms_output.put_line(coalesce(to_char(test.first), 'NULL'));
     12      dbms_output.put_line(coalesce(to_char(test.last), 'NULL'));
     13  end;
     14  /
    0
    NULL
    NULL
    
    1
    1
    1
    
    PL/SQL procedure successfully completed.
    

    EDIT Also note that associative arrays can be sparse. Looping over the numbers between first and last will raise an exception for any collection that is sparse. Instead use first and next like so: (Last and prev to loop the other direction.)

    SQL> declare
      2      type varchar2_100_aa is table of varchar2(100) index by binary_integer;
      3      test varchar2_100_aa;
      4      i binary_integer;
      5  begin
      6      test(1) := 'Hello';
      7      test(100) := 'Good bye';
      8      dbms_output.put_line(test.count);
      9      dbms_output.put_line(coalesce(to_char(test.first), 'NULL'));
     10      dbms_output.put_line(coalesce(to_char(test.last), 'NULL'));
     11      dbms_output.new_line;
     12  --
     13      i := test.first;
     14      while (i is not null) loop
     15          dbms_output.put_line(to_char(i, '999')  || ' - ' || test(i));
     16          i := test.next(i);
     17      end loop;
     18  end;
     19  /
    2
    1
    100
    
       1 - Hello
     100 - Good bye
    
    PL/SQL procedure successfully completed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an associative array which when var dumped looks like this: Array (
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have an associative array assigned to a Smarty variable, something like this: $foo
I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a hierarchically nested associative array. It looks like this: A = {
I currently have an array, created from a database, an example of which looks
I have my XML feed being created from an associative array. Using new DOMDocument('1.0','UTF-8');
I have a page that creates an associative array, then passes it as a
I have an associative array that I want to display using TileList. However, it
i have an associative array in PHP. $myarray = array( a=>News, b=>Articles, c=>images );

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.