检查Oracle中集合的所有记录是否相同?

检查Oracle中集合的所有记录是否相同?,oracle,collections,plsql,nested,Oracle,Collections,Plsql,Nested,我正在编写一个Oracle过程,并声明了一个嵌套数组来存储一些元素。我想看看集合中的所有元素是否相同。 我使用for loop或any Oracle函数打开。您可以对空实例使用multiselect union distinct,然后使用COUNT: SET SERVEROUTPUT ON; DECLARE type num_tab_type is table of number; tab1 num_tab_type := num_tab_type(1,2,3,4); ta

我正在编写一个Oracle过程,并声明了一个嵌套数组来存储一些元素。我想看看集合中的所有元素是否相同。
我使用for loop或any Oracle函数打开。

您可以对空实例使用
multiselect union distinct
,然后使用
COUNT

SET SERVEROUTPUT ON;
DECLARE
   type num_tab_type  is table of number;
   tab1  num_tab_type := num_tab_type(1,2,3,4);
   tab2  num_tab_type := num_tab_type(2,2,2);
   tab_empty num_tab_type := num_tab_type();

   tab num_tab_type;
BEGIN
    tab:= tab1 MULTISET UNION DISTINCT tab_empty;

    DBMS_OUTPUT.put_Line('tab1 ' ||  
                    CASE WHEN tab.COUNT IN(0,1) THEN 'all elements are the same' 
                    ELSE 'not all elements are the same' END);

    tab:= tab2 MULTISET UNION DISTINCT tab_empty;
    DBMS_OUTPUT.put_Line('tab2 ' || 
                  CASE WHEN tab.COUNT IN(0,1)THEN 'all elements are the same' 
                  ELSE 'not all elements are the same' END);
END;
/
输出:

tab1 not all elements are the same
tab2 all elements are the same

您可以对空实例使用
multiselect union distinct
,然后使用
COUNT

SET SERVEROUTPUT ON;
DECLARE
   type num_tab_type  is table of number;
   tab1  num_tab_type := num_tab_type(1,2,3,4);
   tab2  num_tab_type := num_tab_type(2,2,2);
   tab_empty num_tab_type := num_tab_type();

   tab num_tab_type;
BEGIN
    tab:= tab1 MULTISET UNION DISTINCT tab_empty;

    DBMS_OUTPUT.put_Line('tab1 ' ||  
                    CASE WHEN tab.COUNT IN(0,1) THEN 'all elements are the same' 
                    ELSE 'not all elements are the same' END);

    tab:= tab2 MULTISET UNION DISTINCT tab_empty;
    DBMS_OUTPUT.put_Line('tab2 ' || 
                  CASE WHEN tab.COUNT IN(0,1)THEN 'all elements are the same' 
                  ELSE 'not all elements are the same' END);
END;
/
输出:

tab1 not all elements are the same
tab2 all elements are the same
也许您还可以使用以下操作:

DECLARE
   type num_tab_type  is table of number;
   tab1  num_tab_type := num_tab_type(1,2,3,4);

BEGIN
    IF SET(tab1).COUNT = 1 then
        DBMS_OUTPUT.put_Line('All elements tab1 are the same');
    END IF;

END;
/
也许您也在寻找更相反的操作。

也许您也可以使用操作:

DECLARE
   type num_tab_type  is table of number;
   tab1  num_tab_type := num_tab_type(1,2,3,4);

BEGIN
    IF SET(tab1).COUNT = 1 then
        DBMS_OUTPUT.put_Line('All elements tab1 are the same');
    END IF;

END;
/

也许你也在寻找相反的东西。

非常感谢。它正在工作。尽管这是一个正常的要求,但我们不得不在没有for循环的情况下使用它还是有点惊讶。@PranavMohan我希望您可以在循环中检查这一点,如果您真的愿意的话。非常感谢。它正在工作。尽管这是一个正常的要求,但我们不得不在没有for循环的情况下使用它还是有点惊讶。@PranavMohan我希望如果您真的愿意,您可以在循环中检查它。