Checking for a Collection Item in Oracle

I create a simple type:

create or replace TYPE SIMPLE_TYPE AS OBJECT (ID NUMBER(38), NAME VARCHAR2(20));

A simple test:

DECLARE
   TYPE ObjectList IS TABLE OF SIMPLE_TYPE;
   tmp SIMPLE_TYPE := SIMPLE_TYPE(1, 'a');
   o ObjectList := new ObjectList(SIMPLE_TYPE(2, 'a'), SIMPLE_TYPE(3, 'a'));
BEGIN
   IF tmp.EXISTS(tmp) THEN
    dbms_output.put_line('OK, exists.');
   END IF;
END;

I get an exception: PLS-00302: the component "EXISTS" must be declared

But this example works:

DECLARE
   TYPE NumList IS TABLE OF INTEGER;
   n NumList := NumList(1,3,5,7);
BEGIN
   n.DELETE(2);
   IF n.EXISTS(1) THEN
      dbms_output.put_line('OK, element #1 exists.');
   END IF;
   IF n.EXISTS(3) = FALSE THEN
      dbms_output.put_line('OK, element #2 has been deleted.');
   END IF;
   IF n.EXISTS(99) = FALSE THEN
      dbms_output.put_line('OK, element #99 does not exist at all.');
   END IF;
END;

Is it possible to implement the EXISTS method in type SIMPLE_TYPE?

+3
source share
2 answers
tmp SIMPLE_TYPEE := SIMPLE_TYPE(1, 'a');

…

IF tmp.EXISTS(tmp) THEN

You declare tmphow SIMPLE_TYPE, not ObjectList.

SIMPLE_TYPE is a scalar type, not a collection.

You probably want to o.EXISTScheck (what is ObjectList) instead ?

Update:

EXISTS When applied to a collection, it takes an integer index as an argument and checks to see if there is an element with that index (and not its value).

, SIMPLE_TYPE(1, 'a') , :

ObjectList :

CREATE TYPE ObjectList IS TABLE OF SIMPLE_TYPE;

SELECT:

DECLARE
        tmp SIMPLE_TYPE := SIMPLE_TYPE(1, 'a');
        o ObjectList := new ObjectList(SIMPLE_TYPE(2, 'a'), SIMPLE_TYPE(3, 'a'));
        myid INT;
BEGIN
        SELECT  1
        INTO    myid
        FROM    TABLE(o) q
        WHERE   SIMPLE_TYPE(q.id, q.name) = tmp
                AND rownum = 1;
        IF (myid = 1) THEN
                dbms_output.put_line('OK, exists.');
        END IF;
END;
+3

documentation states, EXISTS() . array.exists(3) , array .

, , - , tmp ObjectList. 10 , MEMBER OF. , , , MAP, , .

SQL> create or replace type simple_type as object
  2       ( id number
  3         , name varchar2(30)
  4         , map member function compare return varchar2);
  5  /

Type created.

SQL>
SQL> create or replace type body simple_type as
  2       map member function compare return varchar2
  3       is
  4          return_value integer;
  5       begin
  6          return to_char(id, '0000000')||name;
  7       end compare;
  8  end;
  9  /

Type body created.

SQL>

...

SQL> set serveroutput on size unlimited
SQL>
SQL> declare
  2      type objectlist is table of simple_type;
  3      tmp simple_type := simple_type(1, 'a');
  4      o objectlist := new objectlist(simple_type(2, 'a'), simple_type(3, 'a'));
  5  begin
  6      if tmp MEMBER OF o then
  7          dbms_output.put_line('ok, exists.');
  8      else
  9          dbms_output.put_line('search me');
 10      end if;
 11  end;
 12  /
search me

PL/SQL procedure successfully completed.

SQL>
+7

Source: https://habr.com/ru/post/1716325/


All Articles