Search code examples
plsqloracle11gunionmultiset

multiset union distinct gives "wrong number of types or arguments passed" error


I am using a for loop to pass different values to a cursor, bulk collect the data and append it to the same nested table using MULTISET UNION operator. However, to avoid duplicate data, i tried to use MULTISET UNION DISTINCT and it throws the error PLS-00306: wrong number or types of arguments in call to 'MULTISET_UNION_DISTINCT' The codes works well without DISTINCT. Please let me know if i'm missing anything here. I'm using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

My code is as follows:

DECLARE
TYPE t_search_rec
IS
  RECORD
  (
    search_id   VARCHAR2(200),
    search_name VARCHAR2(240),
    description VARCHAR2(240) );
TYPE t_search_data
IS
  TABLE OF t_search_rec;
  in_user_id       NUMBER;
  in_user_role     VARCHAR2(20);
  in_period     VARCHAR2(20) := 'Sep-15';
  in_search_string VARCHAR2(20) := 'v';
  l_search_tt t_search_data;
  x_search_tt t_search_data;
  v_entity_gl VA_LOGIN_API.t_entity_list ;
  X_RETURN_CODE VARCHAR2(20);
  X_RETURN_MSG  VARCHAR2(2000);
  CURSOR c_vendors_gl(v_entity VARCHAR2)
  IS
    SELECT UNIQUE vendor_id,
      vendor,
      NULL description
    FROM XXPOADASH.XX_VA_PO_LINES
    WHERE period = in_period
    AND entity      = v_entity
    AND upper(vendor) LIKE upper(in_search_string)
      ||'%';
BEGIN
  in_user_role := 'ROLE';
  in_user_id   := 4359;
  VA_LOGIN_API.DATA_ACCESS_PROC( X_RETURN_CODE => X_RETURN_CODE, X_RETURN_MSG => X_RETURN_MSG, IN_PERSON_ID => in_user_id, IN_PERSON_ROLE => in_user_role, X_ACCESSED_ENTITY_LIST => v_entity_gl );
  IF( v_entity_gl.COUNT >0) THEN
    x_search_tt        := t_search_data();
    FOR I IN v_entity_gl.FIRST..v_entity_gl.COUNT
    LOOP
      OPEN c_vendors_gl(v_entity_gl(i));
      FETCH c_vendors_gl BULK COLLECT INTO l_search_tt;
      CLOSE c_vendors_gl;
      x_search_tt := x_search_tt MULTISET
      UNION DISTINCT l_search_tt;
      l_search_tt.delete;
    END LOOP;
    IF x_search_tt.count = 0 THEN
      x_return_msg      := 'No lines found';
    END IF;
  END IF;
  DBMS_OUTPUT.PUT_LINE(x_return_msg);
  DBMS_OUTPUT.PUT_LINE(x_search_tt.count);
EXCEPTION
WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE(x_return_msg);
  DBMS_OUTPUT.PUT_LINE(x_search_tt.count);
  DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;

Solution

  • multiset union distinct requires the elements of the collection to be comparable. In your case the elements are PL/SQL records that are unfortunately not comparable data structures (i.e. PL/SQL provides no build-in mechanism to compare PL/SQL records).

    multiset union works because it doesn't need to compare the elements.

    One possible workaround is to use Oracle object type instead of PL/SQL record. Object type allows you to implement a comparison method required by multiset union distinct.