Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Given this:

DECLARE
  TYPE T_ARRAY IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
  MY_ARRAY T_ARRAY;
  V_COUNT INTEGER;

I would like to do:

BEGIN
  -- ... some code filling the MY_ARRAY array

  -- obviously COUNT_ELEMENTS() does not exists, this is what I'm looking for :-)
  V_COUNT := COUNT_ELEMENTS(MY_ARRAY);

  DBMS_OUTPUT.PUT_LINE('My array containts ' || V_COUNT || ' elements.');
END;

Is there something better than creating a procedure doing a basic loop incrementing a counter? Maybe a PL/SQL native function already does this COUNT_ELEMENTS()?

share|improve this question
add comment

4 Answers 4

up vote 13 down vote accepted

I think this is what you're after:

V_COUNT := MY_ARRAY.COUNT;
share|improve this answer
add comment

Fortunately, I found in the existing PL/SQL code I have to maintain, a working "native" behavior:

V_COUNT := MY_ARRAY.COUNT;

should do the trick.

This one is very hard to find with Google, since "count" is more frequently referring to the SELECT COUNT(...) which can be found in SQL queries...

share|improve this answer
4  
And here's a link to the docs :-) –  Jack Douglas Jan 24 '12 at 15:15
add comment
declare
   type array_t is varray(10) of number(10);
   array array_t := array_t(1,2,3,4,5,6,7,8,9,10);
c number(10):=0;
b number(10):=0;
begin<<outer>>
   for i in 1..array.count loop
    if( mod(i,2)=0)
then
 c:=c+i;
end if;
   end loop;
dbms_output.put_line(c);
begin
    for i in 1..array.count loop
 if( mod(i,2)<>0)
then
 b:=b+i;
end if;
   end loop;
dbms_output.put_line(b);
end;
end outer;
/
share|improve this answer
add comment

A basic method is:

for i in my_array.first .. my_array.last 
loop 
   v_count:=v_count+1;
end loop;
share|improve this answer
    
-1 this will not work for an associative array - see here for why –  Jack Douglas Feb 27 '12 at 12:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.