HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

PL/SQL: best way to count elements in an array?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
elementssqlarraywaycountbest

Problem

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()?

Solution

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...

Code Snippets

V_COUNT := MY_ARRAY.COUNT;

Context

StackExchange Database Administrators Q#11475, answer score: 8

Revisions (0)

No revisions yet.