2

The below is a function that I am creating to accept an array of varchar2 items and return the internal pk of that record which is a NUMBER for each record. I am struggling to get the syntax right to pass an array of type VARCHAR_ARRAY to the simple sql query in the cursor and return the variable of type NUMBER_ARRAY. the Error is on line 8,42 i.e FROM table_name WHERE column_name IN VARCHAR_ARRAY which was passed to the function. Please help me with this error as I am learning plsql.

  create or replace TYPE VARCHAR_ARRAY AS VARRAY(1000000) OF VARCHAR2(1000);
   /

  create or replace TYPE NUMBER_ARRAY AS VARRAY(1000000) OF NUMBER;
   /

  create or replace Function GET_PRODUCT_ID_ARR(V_PRODUCT_NUM_ARR IN VARCHAR_ARRAY)
  RETURN NUMBER_ARRAY 
  IS
     product_id_list number_array := number_array(); 
     CURSOR c1
     IS 
     SELECT cat_map_id 
     FROM mn_cat_map WHERE product_num IN (V_PRODUCT_NUM_ARR) and catalog_type = 'INT';
  v_output NUMBER;   
  BEGIN
      OPEN c1; 
      LOOP
          fetch c1 into product_id_list;
          EXIT WHEN c1%notfound;
          product_id_list.extend;
          product_id_list(product_id_list.count)  := v_output;
         dbms_output.put_line('Product ('||v_output ||'):'||product_id_list(v_output));
      END LOOP;
  Close c1;
  RETURN product_id_list;
  END;
  /
Dmitriy
  • 5,525
  • 12
  • 25
  • 38
Sanjay Rao
  • 547
  • 1
  • 11
  • 22

2 Answers2

1

There is two issues:

  1. You have to cast varray to table:

    CURSOR c1
    IS 
    SELECT cat_map_id 
    FROM mn_cat_map 
    WHERE product_num IN (select column_value from table(V_PRODUCT_NUM_ARR))
      and catalog_type = 'INT';
    
  2. Add bulk collect after fetch:

    LOOP
      fetch c1 bulk collect into product_id_list limit 100;
      EXIT WHEN c1%notfound;
      product_id_list.extend;
      product_id_list(product_id_list.count)  := v_output;
      dbms_output.put_line('Product ('||v_output ||'):'||product_id_list(v_output));
      END LOOP;
    

If you write limit 100, each loop will put 100 records in product_id_list. You can omit limit clause, in this case you will get all records in one fetch.

EDIT
How to see results:

declare 
  myarray varchar_array; 
  my_num_array number_array;
begin 
  myarray := varchar_array(); 
  myarray.extend(2);
  myarray(1) := '151043'; 
  myarray(2) := '2895'; 
  my_num_array := GET_PRODUCT_ID_ARR(myarray);
  for i in 1 .. my_num_array.count loop
    dbms_output.put_line(my_num_array(i)); 
  end loop; 
end;
/
Dmitriy
  • 5,525
  • 12
  • 25
  • 38
  • When i execute this as follows declare myarray varchar_array; begin myarray := varchar_array(); myarray.extend(2); myarray(1) := '151043'; myarray(2) := '2895'; dbms_output.put_line(GET_PRODUCT_ID_ARR(myarray)); end; / ORA-06550: line 8, column 4: PLS-00306: wrong number or types of arguments in call to 'PUT_LINE' PL/SQL: Statement ignored 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action: – Sanjay Rao Dec 08 '16 at 09:17
  • @SanjayRao It is because your function `GET_PRODUCT_ID_ARR` returns an array. `DBMS_OUTPUT.PUT_LINE` can't take array valiable, it requires a string. – Dmitriy Dec 08 '16 at 10:46
  • @SanjayRao I added example to the answer. – Dmitriy Dec 08 '16 at 10:53
  • Thank you for the detailed explanation.it makes it easy to understand. – Sanjay Rao Dec 08 '16 at 16:42
1

What @William is said is quiet true. VARRAY(1000000) is not recommended. Inplace you can create a type of table. However if i follow what you have done, there seems some mistakes in your code. Please see below how you can do it.

Tables Preparation:

create table mn_cat_map(cat_map_id number,
                        product_num varchar2(1000),
                        catalog_type varchar2(10));
/
INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
     VALUES (10, 'A123', 'INT');

INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
     VALUES (2, 'B121', '2Wheer');

INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
     VALUES (3, 'C645', '4Wheer');

COMMIT;

create or replace TYPE VARCHAR_ARRAY AS VARRAY(1000000) OF VARCHAR2(1000);
/
create or replace TYPE NUMBER_ARRAY AS VARRAY(1000000) OF NUMBER;
/

Code: read explainatory comments inline

CREATE OR REPLACE FUNCTION GET_PRODUCT_ID_ARR (V_PRODUCT_NUM_ARR  VARCHAR_ARRAY)
   RETURN NUMBER_ARRAY
IS
   product_id_list   number_array := number_array ();

   CURSOR c1(tbl_list VARCHAR_ARRAY)
   IS
      SELECT cat_map_id
        FROM mn_cat_map
       WHERE product_num  in (select column_value from table(tbl_list)) ---Checking if the item exists in the table with passed collection 
       AND catalog_type = 'INT';

   v_output     NUMBER:= 0;
BEGIN

    --not opening cursor and am not looking for processing any records.
   --OPEN c1(V_PRODUCT_NUM_ARR);

   --passing the input varray to the cursor.
   for i in c1(V_PRODUCT_NUM_ARR)
   loop

    v_output:=v_output + 1;

    product_id_list.extend;

    product_id_list(product_id_list.COUNT):= i.cat_map_id;

     DBMS_OUTPUT.put_line('Product (' || v_output || '):' ||product_id_list(product_id_list.COUNT));


   end loop;

   RETURN product_id_list;
END;
/

Execution:

SQL> select GET_PRODUCT_ID_ARR(VARCHAR_ARRAY('A123','B121','C645')) COl1 from dual;

COL1
--------------------------------------------------------------------------------
NUMBER_ARRAY(10)

Product (1):10
XING
  • 9,608
  • 4
  • 22
  • 38