如何比较oracle中的两个集合值?

How to compare two collections values in oracle?

假设在一个集合(嵌套集合)a 中我有值 (1,2,3,4,5),在另一个集合 b 中我有值 (5,6,7,8,9)。在这两个集合中,5 是常见的。和return5怎么比较?

假设你的意思是 "Collection" a Nested Table 你可以简单地使用

TYPE NumberArray IS TABLE OF INTEGER;    
ret := NumberArray(1,2,3,4,5) MULTISET INTERSECT NumberArray(5,6,7,8,9);

也检查一些其他的Multiset Operators and Multiset Conditions

如果你只需要"compare",即"equal or not"那么你可以简单地使用

IF NumberArray(1,2,3,4,5) = NumberArray(5,6,7,8,9) THEN ...

对于集合,您可以使用 MULTISET INTERSECT 运算符:

CREATE TYPE intlist IS TABLE OF int;

DECLARE
  a intlist := intlist(1,2,3,4,5);
  b intlist := intlist(5,6,7,8,9);
  c intlist;
BEGIN
  c := a MULTISET INTERSECT b;

  FOR i in 1 .. c.COUNT LOOP
    DBMS_OUTPUT.PUT( c(i) || ',' );
  END LOOP;
  DBMS_OUTPUT.NEW_LINE();
END;
/

输出:

5,

I need to compare each value using loop.

遍历一个数组并使用MEMBER OF运算符检查每个元素是否在另一个数组中:

DECLARE
  a intlist := intlist(1,2,3,4,5);
  b intlist := intlist(5,6,7,8,9);
  c intlist;
BEGIN
  c := intlist();
  FOR i IN 1 .. a.COUNT LOOP
    IF a(i) MEMBER OF b THEN
      c.EXTEND;
      c(c.COUNT) := a(i);
    END IF;
  END LOOP;

  FOR i IN 1 .. c.COUNT LOOP
    DBMS_OUTPUT.PUT( c(i) || ',' );
  END LOOP;
  DBMS_OUTPUT.NEW_LINE();
END;
/

还输出:

5,

db<>fiddle