Natty
Natty

Reputation: 151

Oracle Collection function - Not getting expected results

Please have a look at the below code. The 4th element in the collection is null, and hence I am expecting that the variable does not exists. But still, I am getting TRUE when using exist function. Please clarify as to why this is happening?

DECLARE type nt is varray(5) of varchar2(5); nt1 nt := nt(); BEGIN nt1.extend(5); nt1 :=nt('ant','ball','cat',null,'elm'); for i in nt1.first..nt1.last loop dbms_output.put_line(nt1(i)); end loop; if nt1.exists(4) then dbms_output.put_line('TRUE'); end if; END;

Is it enough, If I have just extended that variable, for the exist function to evaluate to true?

Upvotes: 1

Views: 34

Answers (1)

Thorsten Kettner
Thorsten Kettner

Reputation: 95072

The element is there, only its value is null.

With

nt1.extend(5);

you already create 5 entries, so nt1.exists(4) is true. As a varray can have no gaps, exists only tells you if the array is filled up to that position, yet. (And if it is, you can safely access that element.)

if nt1.exists(4) then 
  if nt1(4) is null then
    dbms_output.put_line('Entry 4 is empty (null)');
  else
    dbms_output.put_line('Entry 4 has value' || nt1(4));
  end if;
else
  dbms_output.put_line('Array not filled up till 4th element, yet');
end if;

Upvotes: 1

Related Questions