简体   繁体   中英

PL/SQL Member of with multicolumn record

How can I use member of with a multicolumn collection?

Tried this:

declare
   TYPE t_ref_by_email_rec IS RECORD (
           email varchar(100)
         , ref   number
   );
   TYPE t_ref_by_email IS TABLE OF t_ref_by_email_rec INDEX BY BINARY_INTEGER;
   v_ref_by_email t_ref_by_email := t_ref_by_email();
begin
   if 'email@domain.com' member of v_ref_by_email.email then
      -- Do something
   end if;
end;

No

  • The MEMBER OF operator is only valid for collection data types.
  • TYPE identifier IS TABLE OF type1 INDEX BY type2 is not a collection; it is an associative array.
  • TYPE identifier IS TABLE OF type1 is how you define a collection.

However, even then you cannot use MEMBER OF on a partial attribute of a collection; you would need to use it to match an entire collection element (and it did not appear to work with a record).

Instead, you can use a loop:

DECLARE
   TYPE t_ref_by_email_rec IS RECORD (
           email varchar(100)
         , ref   number
   );
   TYPE t_ref_by_email IS TABLE OF t_ref_by_email_rec;
   v_ref_by_email t_ref_by_email := t_ref_by_email(
     t_ref_by_email_rec( 'a@b', 1 ),
     t_ref_by_email_rec( 'c@d', 2 ),
     t_ref_by_email_rec( 'e@f', 3 ),
     t_ref_by_email_rec( 'g@h', 4 )
   );
BEGIN
  FOR i IN 1 .. v_ref_by_email.COUNT LOOP
    IF 'e@f' <> v_ref_by_email(i).email THEN
      CONTINUE;
    END IF;
    DBMS_OUTPUT.PUT_LINE( 'Found' );
  END LOOP;
END;
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM