The COUNT Function : Varray « Collections « Oracle PL/SQL Tutorial






The COUNT function returns the number of members in a VARRAY.

SQL>
SQL>
SQL> CREATE OR REPLACE TYPE mem_type IS VARRAY(10) of VARCHAR2(15)
  2  /

Type created.

SQL>
SQL>
SQL> CREATE TABLE club (Name VARCHAR2(10),
  2  Address VARCHAR2(20),
  3  City VARCHAR2(20),
  4  Phone VARCHAR2(8),
  5  Members mem_type)
  6  /

Table created.

SQL>
SQL>
SQL>
SQL> INSERT INTO club VALUES ('AL','111 First St.','Mobile',
  2  '222-2222', mem_type('Brenda','Richard'));

1 row created.

SQL>
SQL> INSERT INTO club VALUES ('FL','222 Second St.','Orlando',
  2  '333-3333', mem_type('Gen','John','Steph','JJ'));

1 row created.

SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE PROCEDURE vartest IS
  2  CURSOR fcur IS
  3    SELECT city, members FROM club;
  4  BEGIN
  5    FOR j IN fcur LOOP
  6       IF j.members.exists(1) THEN
  7         dbms_output.put_line(j.City||' has '|| j.members.count||' members');
  8
  9         END IF;
 10      END LOOP;   /* end for j in fcur loop */
 11  END vartest;
 12  /

Procedure created.

SQL> exec vartest;
Mobile has 2 members
Orlando has 4 members

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>
SQL> drop table club;

Table dropped.

SQL> drop type mem_type;

Type dropped.

SQL>








26.2.Varray
26.2.1.Creating a Varray Type
26.2.2.Using VARRAYs
26.2.3.Getting Information on Varrays
26.2.4.VARRAY in action
26.2.5.Inside the loop, you are accessing array elements by their subscripts.
26.2.6.Assign value to VARRAY
26.2.7.Defining our type to be a VARRAY with 10 elements, where each element is a varying character string of up to 15 characters.
26.2.8.CREATE TABLE with a VARRAY
26.2.9.Loading a Table with a VARRAY in It: INSERT VALUEs with Constants
26.2.10.Query VARRAY column
26.2.11.Query table with VARRAY type column by column name
26.2.12.Manipulating the VARRAY with The TABLE Function
26.2.13.If aliases are used, they must be used consistently
26.2.14.Manipulating the VARRAY with The VARRAY Self-join
26.2.15.VARRAY of Cursor
26.2.16.The COUNT Function
26.2.17.LAST and COUNT give the same result for VARRAYs.
26.2.18.A procedure that uses EXISTS and LAST
26.2.19.EXISTS and LAST
26.2.20.Using PL/SQL to Create Functions to Access Elements
26.2.21.The CAST function converts an object type (such as a VARRAY) into a common type that can be queried. Oracle 10g automatically converts the VARRAY without the CAST.
26.2.22.One way to make the 'members' behave like an array is first to include the row number in the result set like this:
26.2.23.Then, the individual array element can be extracted with a WHERE filter:
26.2.24.Extracting individual members of a VARRAY may be accomplished using two other functions: THE and VALUE
26.2.25.Column_value is a built-in function/pseudo-variable that is held over from the DBMS_SQL package
26.2.26.Loop through by using the built-in NEXT, PRIOR.
26.2.27.The subscript of the last element is always equal to the size of the array.
26.2.28.Decreasing the Size of an Array