すべてのプロダクト
Search
ドキュメントセンター

PolarDB:MULTISET UNION演算子を使用する

最終更新日:May 28, 2024

MULTISET UNION演算子は、2つのコレクションを組み合わせて3番目のコレクションを形成します。

署名は次のとおりです。

coll_1 MULTISET UNION [ALL | DISTINCT] coll_2

ここで、coll_1とcoll_2は結合するコレクションの名前を指定します。

ALLキーワードを含めて、重複する要素 (coll_1とcoll_2の両方に存在する要素) を元のコレクションに存在するたびに、結果に1回表示する必要があることを指定します。 これはMULTISET UNIONのデフォルトの動作です。

DISTINCTまたはUNIQUEキーワードを含めて、重複する要素を結果に1回だけ含めるように指定します。

次の例は、MULTISET UNION演算子を使用して、2つのコレクション (collection_1とcollection_2) を3番目のコレクション (collection_3) に結合することを示しています。

DECLARE
    TYPE int_arr_typは番号のテーブルです (2);
    collection_1 int_arr_typ;
    collection_2 int_arr_typ;
    collection_3 int_arr_typ;
    v_results VARCHAR2(50);
開始
    collection_1 := int_arr_typ(10,20、30);
    collection_2 := int_arr_typ(30,40);
    collection_3 := collection_1 MULTISET UNION ALL collection_2;
    DBMS_OUTPUT.PUT_LINE('COUNT: '| | collection_3.COUNT);
    FOR i IN collection_3.FIRST .. collection_3.LASTループ
        IF collection_3(i) はNULL THENです
            v_results := v_results | | 'NULL ';
        ELSE
            v_results := v_results | | collection_3(i) | | ';
        END IF;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('結果:' | | v_results);
エンド;

カウント: 5
結果: 10 20 30 30 40 

結果のコレクションには、collection_1とcollection_2の要素ごとに1つのエントリが含まれます。 DISTINCTキーワードを使用した場合、結果は次のようになります。

DECLARE
    TYPE int_arr_typは番号のテーブルです (2);
    collection_1 int_arr_typ;
    collection_2 int_arr_typ;
    collection_3 int_arr_typ;
    v_results VARCHAR2(50);
開始
    collection_1 := int_arr_typ(10,20、30);
    collection_2 := int_arr_typ(30,40);
    collection_3 := collection_1 MULTISET UNION DISTINCT collection_2;
    DBMS_OUTPUT.PUT_LINE('COUNT: '| | collection_3.COUNT);
    FOR i IN collection_3.FIRST .. collection_3.LASTループ
        IF collection_3(i) はNULL THENです
            v_results := v_results | | 'NULL ';
        ELSE
            v_results := v_results | | collection_3(i) | | ';
        END IF;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('結果:' | | v_results);
エンド;

カウント: 4
結果: 10 20 30 40 

結果のコレクションには、異なる値を持つメンバーのみが含まれます。 次の例では、MULTISET UNION DISTINCT演算子は、同じコレクション内に格納されている重複エントリも削除することに注意してください。

DECLARE
    TYPE int_arr_typは番号のテーブルです (2);
    collection_1 int_arr_typ;
    collection_2 int_arr_typ;
    collection_3 int_arr_typ;
    v_results VARCHAR2(50);
開始
    collection_1 := int_arr_typ(10,20、30,30);
    collection_2 := int_arr_typ(40,50);
    collection_3 := collection_1 MULTISET UNION DISTINCT collection_2;
    DBMS_OUTPUT.PUT_LINE('COUNT: '| | collection_3.COUNT);
    FOR i IN collection_3.FIRST .. collection_3.LASTループ
        IF collection_3(i) はNULL THENです
            v_results := v_results | | 'NULL ';
        ELSE
            v_results := v_results | | collection_3(i) | | ';
        END IF;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('結果:' | | v_results);
エンド;

カウント: 5
結果: 10 20 30 40 50