Oracle从类型号数组中获取distict值

时间:2015-12-08 10:42:32

标签: oracle plsql

我在下面定义了一个数组类型 -

TYPE INPUT_ARRAY_NUM IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;

我已将上述类型的变量定义为 -

temp INPUT_ARRAY_NUM;

使用以下值填充它们 -

temp(0) := 1;
temp(1) := 1;
temp(2) := 3;

如何获得不同的值(1,3)?

3 个答案:

答案 0 :(得分:6)

您可以使用 NESTED TABLE 并使用MULTISET操作。

SQL> SET SERVEROUTPUT ON SQL> DECLARE 2 TYPE t_tab IS TABLE OF NUMBER; 3 l_tab1 t_tab := t_tab(1,1,3,3,5,5); 4 BEGIN 5 l_tab1 := l_tab1 MULTISET INTERSECT DISTINCT l_tab1; 6 FOR i IN l_tab1.first .. l_tab1.last 7 LOOP 8 DBMS_OUTPUT.put_line('Distinct values are '||l_tab1(i)); 9 END LOOP; 10 END; 11 / Distinct values are 1 Distinct values are 3 Distinct values are 5 PL/SQL procedure successfully completed. 操作中的 DISTINCT 关键字会从集合中删除重复项

例如,

{{1}}

答案 1 :(得分:1)

在片段下方将打印数组的不同值您可以使用它。

FOR y IN ( SELECT DISTINCT column_value
FROM TABLE ( CAST (newsites As siteArray ))
)
LOOP
Dbms_Output.Put_Line ('VAlue = ' || y.column_value);
END LOOP;

https://community.oracle.com/thread/648610?start=0&tstart=0

答案 2 :(得分:0)

    Try this. This is one of the method to get distinct array values from input

    set serveroutput on;
    DECLARE
    TYPE INPUT_ARRAY_NUM
    IS
      TABLE OF NUMBER INDEX BY BINARY_INTEGER;
      tab INPUT_ARRAY_NUM;
    BEGIN
      SELECT DISTINCT * BULK COLLECT
      INTO tab
      FROM
        ( SELECT 1 FROM DUAL
        UNION ALL
        SELECT 1 FROM DUAL
        UNION ALL
        SELECT 2 FROM DUAL
        UNION ALL
        SELECT 3 FROM DUAL
        UNION ALL
        SELECT 3 FROM DUAL
        );
      FOR i IN tab.first..tab.last
      LOOP
        dbms_output.put_line(tab(i));
      END LOOP;
    END;

-------------------------------------OUTPUT----------------------------------------
anonymous block completed
1
2
3
-------------------------------------OUTPUT----------------------------------------