The EXISTS method is used to determine whether a specified element exists in a variable array or nested table.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL-compatible mode.
If the nth element of the collection exists, EXISTS(n) returns TRUE. Otherwise, it returns FALSE. If n is out of bounds, EXISTS returns FALSE without raising the predefined exception SUBSCRIPT_OUTSIDE_LIMIT.
For deleted elements, even if DELETE has reserved a placeholder for them, EXISTS(n) returns FALSE.
Here is an example:
obclient> DECLARE
TYPE emp_id IS TABLE OF NUMBER;
n EMP_ID := emp_id(2,4,6,8);
BEGIN
n.DELETE(2); -- Delete the second element.
n.TRIM; -- Trim the last element.
FOR i IN 1..5 LOOP
IF n.EXISTS(i) THEN
DBMS_OUTPUT.PUT_LINE('n(' || i || ') = ' || n(i));
ELSE
DBMS_OUTPUT.PUT_LINE('n(' || i || ') has no value');
END IF;
END LOOP;
END;
/
Query OK, 0 rows affected
n(1) = 2
n(2) has no value
n(3) = 6
n(4) has no value
n(5) has no value
In this example, a nested table is initialized with four elements, the second element is deleted, and the last element is trimmed. The values or status of elements 1 through 6 are then output.